JAVA程序员培训定制课程c09.ppt

上传人:本田雅阁 文档编号:2145514 上传时间:2019-02-21 格式:PPT 页数:32 大小:157.51KB
返回 下载 相关 举报
JAVA程序员培训定制课程c09.ppt_第1页
第1页 / 共32页
JAVA程序员培训定制课程c09.ppt_第2页
第2页 / 共32页
JAVA程序员培训定制课程c09.ppt_第3页
第3页 / 共32页
亲,该文档总共32页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《JAVA程序员培训定制课程c09.ppt》由会员分享,可在线阅读,更多相关《JAVA程序员培训定制课程c09.ppt(32页珍藏版)》请在三一文库上搜索。

1、第九章,基于文本的Java应用程序,2,本章内容,Java命令行参数和系统属性 标准I/O,文件I/O 常用系统类 Collection接口系列 Deprecation类、属性和方法,3,命令行参数,在启动Java应用程序时可以一次性地向应用程序中传递0多个参数-命令行参数 命令行参数使用格式: java ClassName lisa “bily“ “Mr Brown“ 命令行参数被系统以String数组的方式传递给应用程序中的main方法,由参数args接收 public static void main(String args),4,命令行参数用法举例,1 public class Tes

2、t9_1 2 public static void main(String args) 3 for ( int i = 0; i args.length; i+ ) 4 System.out.println(“args“ + i + “ = “ + argsi); 5 6 7 /运行程序Test9_1.java java Test9_1 lisa “bily“ “Mr Brown“ /输出结果: args0 = lisa args1 = bily args2 = Mr Brown,5,系统属性(System Properties),在Java中,系统属性起到替代环境变量的作用(环境变量是平台相

3、关的) 可使用System.getProperties()方法获得一个 Properties类的对象,其中包含了所有可用的系统属性信息 可使用System.getProperty(String name)方法获得特定系统属性的属性值 在命令行运行Java程序时可使用-D选项添加新的系统属性,6,系统属性用法举例,import java.util.Properties; import java.util.Enumeration; public class Test9_2 public static void main(String args) Properties ps = System.get

4、Properties(); Enumeration pn = ps.propertyNames(); while ( pn.hasMoreElements() ) String pName = (String) pn.nextElement(); String pValue = ps.getProperty(pName); System.out.println(pName + “-“ + pValue); / java -DmyProperty=MyValue Test9_2,7,Properties 类,Properties类可实现属性名到属性值的映射,属性名和属性值均为String类型.

5、Properties类的 propertyNames() 方法可以返回以Enumeration类型表示的所有可用系统属性属性名. Properties类的 getProperty(String key)方法获得特定系统属性的属性值. Properties类的load和save方法可以实现将系统属性信息写入文件和从文件中读取属性信息.,8,I/O控制台(Console I/O),System.out 提供向“标准输出”写出数据的功能 System.out为 PrintStream类型. System.in 提供从“标准输入”读入数据的功能 System.in 为InputStream类型. Sy

6、stem.err提供向“标准错误输出”写出数据的功能 System.err为 PrintStream类型.,9,向标准输出写出数据,System.out/System.err的println/print方法 println方法可将方法参数输出并换行 print方法将方法参数输出但不换行 print和println方法针对多数数据类型进行了重写 (boolean, char, int, long, float, double以及char, Object和 String). print(Object)和println(Object)方法中调用了参数的toString()方法,再将生成的字符串输出,

7、10,从标准输入读取数据,import java.io.*; public class Test9_3 public static void main (String args) String s; / 创建一个BufferedReader对象从键盘逐行读入数据 InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); try / 每读入一行后向显示器输出 s = br.readLine(); while (!s.equals(“) System.

8、out.println(“Read: “ + s); s = br.readLine(); br.close(); / 关闭输入流 catch (IOException e) / 捕获可能的IOException. e.printStackTrace(); ,11,Ex1 Inptut/Output,练习M9-4页Example,体会命令行参数的使用; 练习M9-7页Example,理解系统属性的含义和用法; 练习M9-10页Example,初步认识java语言的输入/输出机制;,12,文件输入输出,java.io包中定义了与数据输入、输出功能有关的类,包括提供文件操作功能的File类 创建F

9、ile类对象 File 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,File类方法介绍,关于文件/目录名操作 String getName() String getPath() String getAbsolutePath() String getParent() boolean renameTo

10、(File newName) File 测试操作 boolean exists() boolean canWrite() boolean canRead() boolean isFile() boolean isDirectory() boolean isAbsolute();,获取常规文件信息操作 long lastModified() long length() boolean delete() 目录操作 boolean mkdir() String list(),14,文件I/O有关类型,文件输入 可使用FileReader类以字符为单位从文件中读入数据 可使用BufferedReade

11、r类的readLine方法以行为单位读入一行字符 文件输出 可使用FileWriter类以字符为单位向文件中写出数据 使用PrintWriter类的print和println方法以行为单位写出数据,15,文件输入举例,import java.io.*; public class Test9_4 public static void main (String args) String fname = “Test9_4.java“; File f = new File(fname); try FileReader fr = new FileReader(f); BufferedReader br

12、= 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(); ,16,文件输出举例,import ja

13、va.io.*; public class Test9_5 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.equal

14、s(“) / 从键盘逐行读入数据输出到文件 out.println(s); s = in.readLine(); in.close(); / 关闭BufferedReader输入流. out.close(); / 关闭连接文件的PrintWriter输出流. catch (IOException e) System.out.println(e); ,17,Ex2 File Input/Output,分析并运行M9-15页例子,结合命令行参数的使用,练习从已经存在的文件中读入数据并显示的过程; 分析并运行M9-16页例子,结合命令行参数的使用,练习从标准输入中读入数据并将数据写到文件中的过程;,

15、18,Math类,Math类中定义了多个static方法提供常用数学运算功能 截断操作(Truncation): ceil, floor, round 取最大、最小及绝对值: max, min, abs 三角函数: sin, cos, tan, asin, acos, atan, toDegrees, toRadians 对数运算: log , exp 其它: sqrt, pow, random 常量: PI, E,19,String 类,String 类对象保存不可修改的Unicode字符序列 String类的下述方法能创建并返回一个新的String对象: concat, replace,

16、substring, toLowerCase, toUpperCase, trim,String. 提供查找功能的有关方法: endsWith, startsWith, indexOf,,lastIndexOf. 提供比较功能的方法: equals, equalsIgnoreCase, compareTo. 其它方法: charAt ,length.,20,StringBuffer类,StringBuffer类对象保存可修改的Unicode字符序列 构造方法 StringBuffer() StringBuffer(int capacity) StringBuffer(String initia

17、lString) 实现修改操作的方法: append, insert, reverse, setCharAt, setLength.,21,Collection API,Collection API提供“集合”的功能 Collection API包含下述接口 Collection: 将一组对象以集合元素的形式组织到一起,在其子接口中分别实现不同的组织方式 Set: Collection的子接口,不记录元素的保存顺序,且不允许有重复元素 List: Collection的子接口,记录元素的保存顺序,且允许有重复元素,22,Collection API 层次结构,23,Set 接口用法举例,imp

18、ort java.util.*; public class Test9_6 public static void main(String args) HashSet h = new HashSet(); h.add(“1st“); 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(Set s) System.out.println(s); /本应用

19、程序输出结果如下:1st, 3, 2nd, 4.0,24,List 接口用法举例,import java.util.*; public class Test9_7 public static void main(String args) ArrayList h = new ArrayList(); h.add(“1st“); 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

20、 void m1(List s) System.out.println(s); /本应用程序输出结果如下:1st, 2nd, 3, 4.0, 2nd, 3,25,Iterator接口,Iterator接口定义了对Collection类型对象中所含元素的遍历等增强处理功能 可以通过Collection接口中定义的iterator()方法获得一个对应的Iterator(实现类)对象 Set (实现类)对象对应的Iterator仍然是无序的 List(实现类)对象对应的ListIterator对象可以实现对所含元素的双向遍历: 使用next()方法和previous()方法,26,Iterator接

21、口用法举例,import java.util.*; public class Test9_8 public static void main(String args) ArrayList h = new ArrayList(); h.add(“1st“); h.add(“2nd“); h.add(new Integer(3); h.add(new Double(4.0); Iterator it = h.iterator(); while ( it.hasNext() ) System.out.println(it.next(); ,27,Iterator接口层次,28,Deprecation

22、,Deprecation关键字可用于标记类、属性和方法,表明这些类,属性或方法已过时、不再提倡使用. Deprecation 成分均存在相应的替代类、属性或方法,这些替代者可能采用了更标准化的命名惯例、或功能更适用. 在移植Java代码时,可使用 deprecation 选项获得有关的详细信息. javac -deprecation MyFile.java,29,Deprecation举例(1),public class HelloWorld public static void main(String args) String f; f = System.getenv(“java.class

23、.path“); System.out.println(f); 编译: javac HelloWorld.java输出结果: HelloWorld.java uses or overrides a deprecated API. Recompile with “-deprecation“ for details. 1 warning Process completed.,30,Deprecation举例(2),public class HelloWorld public static void main(String args) String f; f = System.getenv(“jav

24、a.class.path“); System.out.println(f); 再次编译: javac deprecation HelloWorld.java输出结果: HelloWorld.java:4: Note: The method java.lang.String getenv(java.lang.String) in class java.lang.System has been deprecated. f = System.getenv(“java.class.path“); HelloWorld.java uses or overrides a deprecated API. P

25、lease consult the documentation for a better alternative. 1 warning Process completed.,31,Deprecation举例(3),public 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.jar,32,Ex3,编写程序,练习Math,String,StringBuffer类中有关方法的使用; 可参考jdk1.3docs 文档 运行M9-23,24页的例子,体会Set与List接口的区别; 理解M9-29,31页程序,查看jdk1.3docs 文档理清各种数据类型的性能及用法;体会“Deprecation”方法及其处理方式。(选做),

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 其他


经营许可证编号:宁ICP备18001539号-1