1、1第十三课2Java命令行参数和系统属性标准I/O,文件I/O常用系统类Collection接口系列Deprecation类、属性和方法3 在启动Java应用程序时可以一次性地向应用程序中传递0多个参数-命令行参数 命令行参数使用格式:java ClassName lisa bily Mr Brown 命令行参数被系统以String数组的方式传递给应用程序中的main方法,由参数args接收 public static void main(String args)41 public class Test13_1 2 public static void main(String args)3 f
2、or(int i=0;i args.length;i+)4 System.out.println(args+i+=+argsi);56 7 /运行程序Test13_1.javajava Test13_1 lisa bily Mr Brown/输出结果:args0=lisaargs1=bilyargs2=Mr Brown09015 在Java中,系统属性起到替代环境变量的作用(环境变量是平台相关的)可使用System.getProperties()方法获得一个 Properties类的对象,其中包含了所有可用的系统属性信息 可使用System.getProperty(String name)方法
3、获得特定系统属性的属性值 在命令行运行Java程序时可使用-D选项添加新的系统属性6Properties类可实现属性名到属性值的映射,属性名和属性值均为String类型.Properties类的 propertyNamespropertyNames()方法可以返回以Enumeration类型表示的所有可用系统属性属性名.Properties类的 getProperty(String key)方法获得特定系统属性的属性值.Properties类的load和save方法可以实现将系统属性信息写入文件和从文件中读取属性信息.7import java.util.Properties;import ja
4、va.util.Enumeration;public class Test13_2 public static void main(String args)Properties ps=System.getProperties();Enumeration pn=ps.propertyNames();while(pn.hasMoreElements()String pName=(String)pn.nextElement();String pValue=ps.getProperty(pName);System.out.println(pName+-+pValue);/java-DmyPropert
5、y=MyValue Test13_209028 System.out 提供向“标准输出”写出数据的功能System.out为 PrintStream类型.System.in 提供从“标准输入”读入数据的功能System.in 为InputStream类型.System.err提供向“标准错误输出”写出数据的功能System.err为 PrintStream类型.9System.out/System.err的println/print方法println方法可将方法参数输出并换行 print方法将方法参数输出但不换行print和println方法针对多数数据类型进行了重写(boolean,char
6、,int,long,float,double以及char,Object和 String).print(Object)和println(Object)方法中调用了参数的toString()方法,再将生成的字符串输出10import java.io.*;public class Test13_3 public static void main(String args)String s;/创建一个BufferedReader对象从键盘逐行读入数据InputStreamReader isr=new InputStreamReader(System.in);BufferedReader br=new B
7、ufferedReader(isr);try/每读入一行后向显示器输出s=br.readLine();while(!s.equals()System.out.println(Read:+s);s=br.readLine();br.close();/关闭输入流 catch(IOException e)/捕获可能的IOException.e.printStackTrace();090311练习,体会命令行参数的使用;练习,理解系统属性的含义和用法;练习,初步认识java语言的输入/输出机制;12java.io包中定义与数据输入、输出功能有关的类,包括提供文件操作功能的File类创建File类对象F
8、ile f;f=new File(Test.java);f=new File(E:ex,Test.java);在Java中,将目录也当作文件处理File类中提供了实现目录管理功能的方法File path=new File(E:ex);File f=new File(path,Test.java);13 关于文件/目录名操作 String getName()String getPath()String getAbsolutePath()String getParent()boolean renameTo(File newName)File 测试操作 boolean exists()boolean
9、 canWrite()boolean canRead()boolean isFile()boolean isDirectory()boolean isAbsolute();获取常规文件信息操作 long lastModified()long length()boolean delete()目录操作 boolean mkdir()String list()14 文件输入 可使用FileReader类以字符为单位从文件中读入数据 可使用BufferedReader类的readLine方法以行为单位读入一行字符 文件输出 可使用FileWriter类以字符为单位向文件中写出数据 使用PrintWri
10、ter类的print和println方法以行为单位写出数据15 import java.io.*;public class Test13_4 public static void main(String args)String fname=Test13_4.java;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=b
11、r.readLine();br.close();/关闭缓冲读入流及文件读入流的连接.catch(FileNotFoundException e1)System.err.println(File not found:+fname);catch(IOException e2)e2.printStackTrace();16import java.io.*;public class Test13_5 public static void main(String args)File file=new File(tt.txt);try InputStreamReader is=new InputStrea
12、mReader(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)System.out.pr
13、intln(e);17结合命令行参数的使用,练习从已经存在的文件中读入数据并显示的过程;结合命令行参数的使用,练习从标准输入中读入数据并将数据写到文件中的过程;18 Math类中定义了多个static方法提供常用数学运算功能 截断操作(Truncation):ceil,floor,round 取最大、最小及绝对值:max,min,abs 三角函数:sin,cos,tan,asin,acos,atan,toDegrees,toRadians 对数运算:log,exp 其它:sqrt,pow,random 常量:PI,E19String 类对象保存不可修改的Unicode字符序列 String类的
14、下述方法能创建并返回一个新的String对象:concat,replace,substring,toLowerCase,toUpperCase,trim,String.提供查找功能的有关方法:endsWith,startsWith,indexOf,,lastIndexOf.提供比较功能的方法:equals,equalsIgnoreCase,compareTo.其它方法:charAt,length.20 StringBuffer类对象保存可修改的Unicode字符序列 构造方法 StringBuffer()StringBuffer(int capacity)StringBuffer(String
15、 initialString)实现修改操作的方法:append,insert,reverse,setCharAt,setLength.21编写程序,练习Math,String,StringBuffer类中有关方法的使用;可参考jdk1.3docs 文档22 Collection API提供“集合”的功能 Collection API包含下述接口 Colection:将一组对象以集合元素的形式组织到一起,在其子接口中分别实现不同的组织方式 Set:Collection的子接口,不记录元素的保存顺序,且不允许有重复元素 List:Collection的子接口,记录元素的保存顺序,且允许有重复元素2
16、3Collection+add(element:Object):boolean+remove(element:Object):boolean+size():boolean+isEmpty():boolean+contains(element:Object):boolean+iterator():IteratorSetHashSetListArrayListVector24import java.util.*;public class Test13_6 public static void main(String args)HashSet h=new HashSet();h.add(1st);h
17、.add(2nd);h.add(new Integer(3);h.add(new Double(4.0);h.add(2nd);/重复元素,未被加入h.add(new Integer(3);/重复元素,未被加入m1(h);public static void m1(Set s)System.out.println(s);/本应用程序输出结果如下:1st,3,2nd,4.025import java.util.*;public class Test13_7public static void main(String args)ArrayList h=new ArrayList();h.add(1
18、st);h.add(2nd);h.add(new Integer(3);h.add(new Double(4.0);h.add(2nd);/重复元素,加入h.add(new Integer(3);/重复元素,加入m1(h);public static void m1(List s)System.out.println(s);/本应用程序输出结果如下:1st,2nd,3,4.0,2nd,326Deprecation关键字可用于标记类、属性和方法,表明这些类,属性或方法已过时、不再提倡使用.Deprecation 成分均存在相应的替代类、属性或方法,这些替代者可能采用了更标准化的命名惯例、或功能更
19、适用.在移植Java代码时,可使用 deprecation 选项获得有关的详细信息.javac-deprecation MyFile.java27public class HelloWorld public static void main(String args)String f;f=System.getenv(java.class.path);System.out.println(f);编译:javac HelloWorld.java输出结果:HelloWorld.java uses or overrides a deprecated API.Recompile with-deprecat
20、ion for details.1 warningProcess completed.28public class HelloWorld public static void main(String args)String f;f=System.getenv(java.class.path);System.out.println(f);再次编译:javac deprecation HelloWorld.java输出结果:HelloWorld getenv(java.lang.String)in class java.lang.System has been deprecated.f=Syste
21、m.getenv(java.class.path);HelloWorld.java uses or overrides a deprecated API.Please consult the documentation for a better alternative.1 warningProcess completed.29public class HelloWorld public static void main(String args)String f;f=System.getProperty(java.class.path);System.out.println(f);修改后编译通过运行输出结果:D:lgzex;C:jdk1.3jrelibrt.jar;C:jdk1.3jrelibi18n.jar;C:jdk1.3libdt.jar;C:jdk1.3libtools.jar30体会Set与List接口的区别;查看jdk1.3docs 文档理清各种数据类 型 的 性 能 及 用 法;体 会“Deprecation”方法及其处理方式。(选做)31总结 课程总结