收藏 分销(赏)

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

上传人:w****g 文档编号:4666713 上传时间:2024-10-08 格式:PPTX 页数:31 大小:193.65KB 下载积分:12 金币
下载 相关 举报
JAVA程序员培训定制课程c09.pptx_第1页
第1页 / 共31页
JAVA程序员培训定制课程c09.pptx_第2页
第2页 / 共31页


点击查看更多>>
资源描述
1本章内容Java命令行参数和系统属性标准I/O,文件I/O常用系统类Collection接口系列Deprecation类、属性和方法2命令行参数在启动Java应用程序时可以一次性地向应用程序中传递0多个参数-命令行参数命令行参数使用格式:java ClassName lisa bily Mr Brown命令行参数被系统以String数组的方式传递给应用程序中的main方法,由参数args接收 public static void main(String args)3命令行参数用法举例1 public class Test9_1 2 public static void main(String args)3 for(int i=0;i args.length;i+)4 System.out.println(args+i+=+argsi);56 7 /运行程序Test9_1.javajava Test9_1 lisa bily Mr Brown/输出结果:args0=lisaargs1=bilyargs2=Mr Brown4系统属性(System Properties)在Java中,系统属性起到替代环境变量的作用(环境变量是平台相关的)可 使 用 System.getProperties()方 法 获 得 一 个 Properties类的对象,其中包含了所有可用的系统属性信息可使用System.getProperty(String name)方法获得特定系统属性的属性值在命令行运行Java程序时可使用-D选项添加新的系统属性5系统属性用法举例import java.util.Properties;import java.util.Enumeration;public class Test9_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-DmyProperty=MyValue Test9_26Properties 类Properties类可实现属性名到属性值的映射,属性名和属性值均为String类型.Properties类的 propertyNamespropertyNames()方法可以返回以Enumeration类型表示的所有可用系统属性属性名.Properties类的 getProperty(String key)方法获得特定系统属性的属性值.Properties类的load和save方法可以实现将系统属性信息写入文件和从文件中读取属性信息.7I/O控制台(Console I/O)System.out 提供向“标准输出”写出数据的功能System.out为 PrintStream类型.System.in 提供从“标准输入”读入数据的功能System.in 为InputStream类型.System.err提供向“标准错误输出”写出数据的功能System.err为 PrintStream类型.8向标准输出写出数据System.out/System.err的println/print方法println方法可将方法参数输出并换行 print方法将方法参数输出但不换行print和println方法针对多数数据类型进行了重写(boolean,char,int,long,float,double以及char,Object和 String).print(Object)和 println(Object)方 法 中 调 用 了 参 数 的 toString()方法,再将生成的字符串输出9从标准输入读取数据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.out.println(Read:+s);s=br.readLine();br.close();/关闭输入流 catch(IOException e)/捕获可能的IOException.e.printStackTrace();10Ex1 Inptut/Output1.练习M9-4页Example,体会命令行参数的使用;2.练习M9-7页Example,理解系统属性的含义和用法;3.练习M9-10页Example,初步认识java语言的输入/输出机制;11文件输入输出java.io包中定义了与数据输入、输出功能有关的类,包括提供文件操作功能的File类创建File类对象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);12File类方法介绍 关于文件/目录名操作 String getName()String getPath()String getAbsolutePath()String getParent()boolean renameTo(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()13文件I/O有关类型文件输入可使用FileReader类以字符为单位从文件中读入数据可使用BufferedReader类的readLine方法以行为单位读入一行字符文件输出可使用FileWriter类以字符为单位向文件中写出数据使用PrintWriter类的print和println方法以行为单位写出数据14文件输入举例 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=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();15文件输出举例import java.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.equals()/从键盘逐行读入数据输出到文件out.println(s);s=in.readLine();in.close();/关闭BufferedReader输入流.out.close();/关闭连接文件的PrintWriter输出流.catch(IOException e)System.out.println(e);16Ex2 File Input/Output1.分析并运行M9-15页例子,结合命令行参数的使用,练习从已经存在的文件中读入数据并显示的过程;2.分析并运行M9-16页例子,结合命令行参数的使用,练习从标准输入中读入数据并将数据写到文件中的过程;17Math类Math类中定义了多个static方法提供常用数学运算功能 截断操作(Truncation):ceil,floor,round 取最大、最小及绝对值:max,min,abs 三角函数:sin,cos,tan,asin,acos,atan,toDegrees,toRadians 对数运算:log,exp 其它:sqrt,pow,random 常量:PI,E18String 类String 类对象保存不可修改的Unicode字符序列 String类的下述方法能创建并返回一个新的String对象:concat,replace,substring,toLowerCase,toUpperCase,trim,String.提供查找功能的有关方法:endsWith,startsWith,indexOf,,lastIndexOf.提供比较功能的方法:equals,equalsIgnoreCase,compareTo.其它方法:charAt,length.19StringBuffer类StringBuffer类对象保存可修改的Unicode字符序列构造方法 StringBuffer()StringBuffer(int capacity)StringBuffer(String initialString)实现修改操作的方法:append,insert,reverse,setCharAt,setLength.20Collection APICollection API提供“集合”的功能Collection API包含下述接口Collection:将一组对象以集合元素的形式组织到一起,在其子接口中分别实现不同的组织方式Set:Collection的子接口,不记录元素的保存顺序,且不允许有重复元素List:Collection的子接口,记录元素的保存顺序,且允许有重复元素21Collection API 层次结构Collection+add(element:Object):boolean+remove(element:Object):boolean+size():boolean+isEmpty():boolean+contains(element:Object):boolean+iterator():IteratorSetHashSetListArrayListVector22Set 接口用法举例import 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);/本应用程序输出结果如下:1st,3,2nd,4.023List 接口用法举例import java.util.*;public class Test9_7public 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 void m1(List s)System.out.println(s);/本应用程序输出结果如下:1st,2nd,3,4.0,2nd,324Iterator接口Iterator接口定义了对Collection类型对象中所含元素的遍历等增强处理功能可以通过Collection接口中定义的iterator()方法获得一个对应的Iterator(实现类)对象Set(实现类)对象对应的Iterator仍然是无序的List(实现类)对象对应的ListIterator对象可以实现对所含元素的双向遍历:使用next()方法和previous()方法25Iterator接口用法举例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();26Iterator接口层次Iterator+hasNext():boolean+next():boolean+remove()ListIterator+hasPrevious():boolean+previous():Object+add(element:Object)+set(element :Object)27DeprecationDeprecation关键字可用于标记类、属性和方法,表明这些类,属性或方法已过时、不再提倡使用.Deprecation 成分均存在相应的替代类、属性或方法,这些替代者可能采用了更标准化的命名惯例、或功能更适用.在移植Java代码时,可使用 deprecation 选项获得有关的详细信息.javac-deprecation MyFile.java28Deprecation举例(1)public 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-deprecation for details.1 warningProcess completed.29Deprecation举例(2)public 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.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.Please consult the documentation for a better alternative.1 warningProcess completed.30Deprecation举例(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.jar31Ex3 1.编写程序,练习Math,String,StringBuffer类中有关方法的使用;可参考jdk1.3docs 文档2.运行M9-23,24页的例子,体会Set与List接口的区别;3.理解M9-29,31页程序,查看jdk1.3docs 文档理清各种数据类型的性能及用法;体会“Deprecation”方法及其处理方式。(选做)
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服