Java精解案例教程 第9课 基于文本的Java应用程序.ppt_第1页
Java精解案例教程 第9课 基于文本的Java应用程序.ppt_第2页
Java精解案例教程 第9课 基于文本的Java应用程序.ppt_第3页
Java精解案例教程 第9课 基于文本的Java应用程序.ppt_第4页
Java精解案例教程 第9课 基于文本的Java应用程序.ppt_第5页
已阅读5页,还剩22页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第九章 基于文本的Java应用程序 本章内容 Java命令行参数和系统属性标准I O 文件I O常用系统类Collection接口系列 命令行参数 在启动Java应用程序时可以一次性地向应用程序中传递0 多个参数 命令行参数命令行参数使用格式 javaClassNamelisa bily MrBrown 命令行参数被系统以String数组的方式传递给应用程序中的main方法 由参数args接收publicstaticvoidmain String args 命令行参数用法举例 1publicclassTest9 1 2publicstaticvoidmain String args 3for inti 0 i args length i 4System out println args i args i 5 6 7 运行程序0901 Test9 1 javajavaTest9 1lisa bily MrBrown 输出结果 args 0 lisaargs 1 bilyargs 2 MrBrown 系统属性 SystemProperties 在Java中 系统属性起到替代环境变量的作用 环境变量是平台相关的 可使用System getProperties 方法获得一个Properties类的对象 其中包含了所有可用的系统属性信息可使用System getProperty Stringname 方法获得特定系统属性的属性值在命令行运行Java程序时可使用 D选项添加新的系统属性 Properties类 Properties类可实现属性名到属性值的映射 属性名和属性值均为String类型 Properties类的propertyNames 方法可以返回以Enumeration类型表示的所有可用系统属性属性名 Properties类的getProperty Stringkey 方法获得特定系统属性的属性值 Properties类的load和save方法可以实现将系统属性信息写入文件和从文件中读取属性信息 系统属性用法举例 0902 Project importjava util Properties importjava util Enumeration publicclassTest9 2 publicstaticvoidmain String args Propertiesps System getProperties Enumerationpn ps propertyNames while pn hasMoreElements StringpName String pn nextElement StringpValue ps getProperty pName System out println pName pValue I O控制台 ConsoleI O System out提供向 标准输出 写出数据的功能System out为PrintStream类型 System in提供从 标准输入 读入数据的功能System in为InputStream类型 System err提供向 标准错误输出 写出数据的功能System err为PrintStream类型 0903 向标准输出写出数据 System out System err的println print方法println方法可将方法参数输出并换行print方法将方法参数输出但不换行print和println方法针对多数数据类型进行了重写 boolean char int long float double以及char Object和String print Object 和println Object 方法中调用了参数的toString 方法 再将生成的字符串输出 0904 Mydate java 从标准输入读取数据 importjava io publicclassTest9 3 publicstaticvoidmain Stringargs Strings 创建一个BufferedReader对象从键盘逐行读入数据InputStreamReaderisr newInputStreamReader System in BufferedReaderbr newBufferedReader isr try 每读入一行后向显示器输出s br readLine while s equals System out println Read s s br readLine br close 关闭输入流 catch IOExceptione 捕获可能的IOException e printStackTrace 文件输入输出 java io包中定义与数据输入 输出功能有关的类 包括提供文件操作功能的File类创建File类对象Filef f newFile Test java f newFile E ex Test java 在Java中 将目录也当作文件处理File类中提供了实现目录管理功能的方法Filepath newFile E ex Filef newFile path Test java File类方法介绍 0905 Test java 关于文件 目录名操作StringgetName StringgetPath StringgetAbsolutePath StringgetParent booleanrenameTo FilenewName File测试操作booleanexists booleancanWrite booleancanRead booleanisFile booleanisDirectory booleanisAbsolute 获取常规文件信息操作longlastModified longlength booleandelete 目录操作booleanmkdir String list 文件I O有关类型 文件输入可使用FileReader类以字符为单位从文件中读入数据可使用BufferedReader类的readLine方法以行为单位读入一行字符文件输出可使用FileWriter类以字符为单位向文件中写出数据使用PrintWriter类的print和println方法以行为单位写出数据 文件输入举例 0906 importjava io publicclassTest9 4 publicstaticvoidmain String args Stringfname Test9 4 java Filef newFile fname try FileReaderfr newFileReader f BufferedReaderbr newBufferedReader fr Strings br readLine while s null System out println 读入 s s br readLine br close 关闭缓冲读入流及文件读入流的连接 catch FileNotFoundExceptione1 System err println Filenotfound fname catch IOExceptione2 e2 printStackTrace 文件输出举例 0906 importjava io publicclassTest9 5 publicstaticvoidmain String args Filefile newFile tt txt try InputStreamReaderis newInputStreamReader System in BufferedReaderin newBufferedReader is PrintWriterout newPrintWriter newFileWriter file Strings in readLine while s equals 从键盘逐行读入数据输出到文件out println s s in readLine in close 关闭BufferedReader输入流 out close 关闭连接文件的PrintWriter输出流 catch IOExceptione System out println e Math类 Math类中定义了多个static方法提供常用数学运算功能 0907 截断操作 Truncation ceil floor round取最大 最小及绝对值 max min abs三角函数 sin cos tan asin acos atan toDegrees toRadians对数运算 log exp其它 sqrt pow random常量 PI E String类 0908 String类对象保存不可修改的Unicode字符序列String类的下述方法能创建并返回一个新的String对象 concat replace substring toLowerCase toUpperCase trim 提供查找功能的有关方法 endsWith startsWith indexOf lastIndexOf 提供比较功能的方法 equals equalsIgnoreCase compareTo 其它方法 charAt length StringBuffer类 0909 StringBuffer类对象保存可修改的Unicode字符序列构造方法StringBuffer StringBuffer intcapacity StringBuffer StringinitialString 实现修改操作的方法 append insert reverse setCharAt setLength CollectionAPI CollectionAPI提供 集合 的功能CollectionAPI包含下述接口Colection 将一组对象以集合元素的形式组织到一起 在其子接口中分别实现不同的组织方式Set Collection的子接口 不记录元素的保存顺序 且不允许有重复元素List Collection的子接口 记录元素的保存顺序 且允许有重复元素 CollectionAPI层次结构 Set接口用法举例 0910 importjava util publicclassTest9 6 publicstaticvoidmain String args HashSeth newHashSet h add 1st h add 2nd h add newInteger 3 h add newDouble 4 0 h add 2nd 重复元素 未被加入h add newInteger 3 重复元素 未被加入m1 h publicstaticvoidm1 Sets System out println s 本应用程序输出结果如下 1st 3 2nd 4 0 List接口用法举例 0910 importjava util publicclassTest9 7 publicstaticvoidmain String args ArrayListh newArrayList h add 1st h add 2nd h add newInteger 3 h add newDouble 4 0 h add 2nd 重复元素 加入h add newInteger 3 重复元素 加入m1 h publicstaticvoidm1 Lists System out println s 本应用程序输出结果如下 1st 2nd 3 4 0 2nd 3 Iterator接口 Iterator接口定义了对Collection类型对象中所含元素的遍历等增强处理功能可以通过Collection接口中定义的iterator 方法获得一个对应的Iterator 实现类 对象Set 实现类 对象对应的Iterator仍然是无序的List 实现类 对象对应的ListIterator对象可以实现对所含元素的双向遍历 使用next 方法和previous 方法 Iterator接口用法举例 importjava ut

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论