`
fehly
  • 浏览: 245400 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

控制台应用程序设计

    博客分类:
  • java
阅读更多

命令行参数

  • 在启动时java控制台应用程序,可以一次性地向程序中传递零至多个字符串参数,这些参数被称为命令行参数.
  • 用法格式:
  • java <应用程序类名> [<命令行参数>]*
  • 命令行参数将被系统接收并静态初始化为一个一维的String数组对象,然后将之作为实参传给应用程序入口方法main()
  • 命令行参数须使用空格符及双引号分隔
public class TestCommandLineArgs{
	public static void main(String[] args){
		for(int i=0;i<args.length;i++){
			System.out.println(args[i]);	
		}	
	}	
}
使用下述命令运行程序:
java TestCommandLineArgs Lisa "Billy" "Mr Brown" "a""b"
输出结果:
Lisa
Billy
Mr Brown
a"b

系统属性

  • java系统属性以"键-值"对的形式存在——由属性名称、属性值两部分组成,均为字符串形式,记录了当前操作系统和JVM等相关的环境信息。
  • 可使用System.getProperties()方法获得Properties类的对象,其中包含了所有可用的系统属性信息
  • 可使用System或Properties类的getProperty(String)方法获得特定系统属性的属性值
  • 可使用System或Properties类的setProperty(String,String)方法添加新的属性信息
  • 在命令行运行java程序时,可使用-D属性添加新的系统属性
import java.util.Properties;
import java.util.Enumeration;
public class TestSystemProperties {
	public static void main(String[] args) {
		Properties ps = System.getProperties();
		ps.setProperty("*****myName*****","*****myValue*****");		
		Enumeration pn = ps.propertyNames();
		while ( pn.hasMoreElements() ) {
			String pName = (String) pn.nextElement();
			String pValue = ps.getProperty(pName);
			System.out.println(pName + "----" + pValue);
		}
	}
}
也可以使用下述命令在运行程序时添加新的系统属性
C:\Test> java    -Dmmmm=vvvv   TestSystemProperties

 标准输入/输出

控制台输入/输出时应用程序的基本功能:

  • System.out提供向"标准输出"写出数据的功能(java.io.PrintStream类型)
  • System.in提供从"标准输入"读入数据的功能(java.io.InputStream类型)
  • System.err提供向"标准错误输出"写出数据的功能(java.io.PrintStream类型)

PrintStream类的主要方法:

  • print()/println()方法被进行了多次重载(boolean,char,int,long,float,double以及char[],Object和String)
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;

public class TestStandardInput{
	public static void main (String args[]) {
		String s;
		InputStreamReader isr = new InputStreamReader(System.in);
		BufferedReader br = new BufferedReader(isr);
		try {
			s = br.readLine();
			while (!s.equals("")) {  	
				System.out.println("Read: " + s);
				s = br.readLine();
			}
			br.close(); 
		} catch (IOException e) { 
			e.printStackTrace();
		}
	}
}

 文件输入输出

  • java.io包中定义与数据输入、输出功能有关的类,包括提供文件操作功能的File类
  • 创建File类对象
  • File f;
    f=new File("Test.java");
    f=new File("E:\\test\\","Test.java")
     
  • 在java中,将目录也当作文件处理File类中提供了实现目录管理功能的方法
  • File path=new File("C:\\test\\");
    File f=new File(path,"Test.java")
     

File类主要方法

关于文件/目录操作

  • String getName()
  • String getPath()
  • String getAbsolutePath()
  • String getParent()

测试操作

  • boolean exists()
  • boolean canWrite()
  • boolean canRead()
  • boolean isFile()
  • boolean isDirectory()
  • boolean isAbsolute()

获取常规文件信息操作

  • long lastModified()
  • long length()

设置和修改操作

  • boolean delete()
  • void deleteOnExit()
  • boolean createNewFile()
  • setReadOnly()
  • boolean rennameTo(File dest)

目录操作

  • boolean mkdir()
  • String[] list()
  • File[] listFiles()
import java.io.File;
import java.util.Date;
public class TestFile{
	public static void main(String[] args) {
		File f1 = new File("a.jpg");
		File f2 = new File("D:\\temp\\","moved.jpg");
		File f3 = new File("D:\\temp\\b.txt");
		File f4 = new File("D:\\ex\\");		
		
		System.out.println("Name: " + f1.getName());
		System.out.println("Path: " + f1.getPath());
		System.out.println("AbsolutePath: " + f1.getAbsolutePath());
		System.out.println("Parent: " + f1.getParent());
		System.out.println("lastModified: " + new Date(f1.lastModified()));
		System.out.println("length: " + f1.length());
		System.out.println("exists: " + f1.exists());
		System.out.println("canRead: " + f1.canRead());
		System.out.println("canWrite: " + f1.canWrite());
		System.out.println("isFile: " + f1.isFile());
		System.out.println("isHidden: " + f1.isHidden());
		System.out.println("isDirectory: " + f1.isDirectory());
		System.out.println("isAbsolute: " + f1.isAbsolute());
		System.out.println("length: " + f1.length());
		
		f3.delete();
		try{
			System.out.println("createNewFile: " 
				+ new File("c.txt").createNewFile());
		}catch(java.io.IOException e){
			e.printStackTrace();	
		}
		
		f1.setReadOnly();
		f1.renameTo(f2);
		System.out.println("createNewFile: " 
			+ new File("D:\\newPath\\").mkdir());
		System.out.println("listFiles:");
		for(File f : f4.listFiles()){
			System.out.println("    -" + f.getName());
		}
	}
}

 文件I/O有关类型

  • java.io.FileReader类——:提供read()方法以字符为单位从文件中读入数据。
  • java.io.FileWriter类——:提供write()方法以字符为单位向文件写出数据
  • java.io.BufferedReader类——:提供readLine()方法以行为单位读入一行字符
  • java.io.PrintWriter类——:提供print()和println()方法为行为单位写出数据

读取文件信息

import java.io.File;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;

public class ReadFile{
	public static void main (String[] args) {
		String fname = "test.txt";
		File f = new File(fname);
		try {
			FileReader fr = new FileReader(f); 
			BufferedReader br = new BufferedReader(fr);
			String s = br.readLine();
			while ( s != null ) {
				System.out.println("读入: " + s);
				s = br.readLine();
			}			
			br.close();
		} catch (FileNotFoundException e1) {
			System.err.println("File not found: " + fname);
		} catch (IOException e2) {
			e2.printStackTrace();
		}
	}
}

 输入信息到文件

import java.io.*;

public class TestWriteFile{
	public static void main (String[] args) {
		File file = new File("tt.txt");
		try {
			InputStreamReader  is = new InputStreamReader(System.in);
			BufferedReader in=new BufferedReader(is);
			PrintWriter out  = new PrintWriter(new FileWriter(file));
			String s = in.readLine();
			while (!s.equals("")) {// 从键盘逐行读入数据输出到文件
				out.println(s);
				s = in.readLine();
			}
			in.close();   // 关闭BufferedReader输入流.		     
			out.close();  // 关闭连接文件的PrintWriter输出流.
		}catch (IOException e) {
			e.printStackTrace();  
		}
	 }
}

 可变参数方法

  • java语言允许在定义方法时指定使用任意数量的参数,其格式是在参数类型后加"..."。
  • 可变长度参数必须放在参数列表的最后,而且一个方法最多只能包含一个可变长度参数。
import java.util.Date;

public class TestVarargs{
	public static void main(String[] args){
		TestVarargs tv = new TestVarargs();
		tv.myprint("china",5000,new Integer(54),new Date(),5,7.89);
	}		

	public void myprint(String s,int i,Object... objs){
		System.out.println(s.toUpperCase());
		System.out.println(100 * i);
		for(Object o: objs){
			System.out.println(o);	
		}
	}	
}

 过时API

  • 过时API是指那些过去定义的,现已不提倡使用的API,包括类,属性和方法等。
  • 过时API存在在相应的替代物,这些替代者可能采用了更标准化的命名惯例,或功能更使用
  • 在移植java代码是,可使用 -Xlint:deprecation选项进行编译以获得相关详细信息
import java.util.*;

public class TestDeprecation{
	public static void main(String[] args){
		Date now = new Date();
		int hour = now.getHours();
		System.out.println(hour);	
	}	
}
编译程序时输出提示信息:
注意:D:\test\TestDeprecation.java使用或覆盖了已过时的API
注意:要了解详细信息,请使用  -Xlint:deprecation重新编译

 用户自定义过时API

在过时的成分前添加第三种形式的注解,并在其中使用"@deprecated"标记标明此成分已过时,同时还可以给出简要的说明信息。

public class A{
	/**
	 @deprecated 本方法不推荐使用,这里只是用于测试用途。
	*/
	public void ma(){
		System.out.println("In class A,just for test!");	
	}	
}
public class TestMyDeprecation{
	public static void main(String[] args){
		A a = new A();
		a.ma();	
	}	
}

注解

  • 注解不直接影响程序的语义,然而开发和部署工具可以对其读取并以某种形式处理。
  • 本质上,注解就是可以添加到代码中的一种类似于修饰符的成分,可以用于声明包,类,构造方法,方法,属性,参数和变量等场合。
  • java语言采用了一类新的数据类型来描述注解——注解类型相当于类或接口,每一条注解相当于该类的一挂实例
  • 注解类型采用"@interface"标记声明

Override注解

java.lang.Override类型注解用于指明被注解的方法重写了父类中的方法,如果不是合法的方法重写则会报错

//import java.lang.Override;
public class Person{
	private String name;
	public Person(String name){
		this.name = name;
	}
	public void setName(String name){
		this.name = name;	
	}	
	public String getName(){
		return name;
	}
	@Override
	public String tostring(){
		return "Name: " + name; 	
	}	
}

 Deprecated注解

Deprecated注解的作用也是标记过时API

SuppressWarnings注解

使用SuppressWarnings注解可以关闭编译器对指定的一种或多种问题的提示/警告功能

import java.util.*;
//import java.lang.SuppressWarnings;
@SuppressWarnings(value={"deprecation"})
public class TestSuppressWarnings{
	public static void main(String[] args){
		Date now = new Date();
		int hour = now.getHours();
		System.out.println(hour);	
	}	
}

语法格式

  • @SuppressWarnings(value={"deprecation"})
  • @SuppressWarnings(value={"deprecation","unchecked"})
  • @SuppressWarnings("deprecation")
  • @uppressWarnings({"deprecation","unchecked"})

归档工具

  • java归档工具是JDK中提供的一种多用途的存档及压缩工具,可以将多个文件或目录合并/压缩为单个的Java归档文件(jar,java archive).
  • jar文件的主要作用:
  • 发布和使用类库
  • 作为程序组件或者插件程序的基本部署单位
  • 用于打包与组件相关联的资源文件

D:\test>jar -cvf Test.jar *.*
--------------------------------------------------------->
jar {ctxui}[vfm0Me] [jar-file] [manifest-file] [entry-point] [-C dir] files..

 

 

 

 



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics