




已阅读5页,还剩29页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第11章I O流 熟练使用File类的常用方法理解流 及其层次结构熟练使用字节流和字符流实现文件读写操作 11 1File类简介 File类为其他IO流类提供了许多有用的辅助功能 是一个辅助类 并提供了在指定目录下创建或者是删除目录 文件的普通的成员方法 但File类不能进行文件的读写操作 同事还可以对指定的磁盘文件进行许多测试包括可读写性 是否是隐藏文件 对指定目录下的文件进行过滤等功能 具体的方法请见帮助文档 并进行测试 11 2File类举例 Filef newFile my txt f createNewFile f mkdir Filef newFile F myjavaclass8 1 txt f createNewFile FilefDir newFile File separator myjavaclass8 StringstrFile File separator 1 txt Filef newFile fDir strFile f createNewFile File arrFile f listFiles File arrFile f listFiles newTest publicbooleanaccept Filedir Stringname returnname indexOf java 1 11 3流的概念 流是程序和外界进行数据交换的通道 它是一种抽象观念 如从键盘输入数据 将结果输出到显示器 打开与保存文件等操作皆视为流的处理 我们可通过它来读写数据 甚至可以通过它连接数据来源 datasource 并可以将数据保存 使用流的好处是 能非常灵活地在文件中保存数据或从文件中读取数据 11 3 1stream 流 流是指一连串流动的数据信号 是以先进先出的方式发送和接收数据的通道 InputStream 来自数据源的数据流 OutputStream 流向目的地的数据流 11 3 2流 续 11 4流相关类的层次结构 Java有一套完整的输入 输出类层次结构 它们是建立在4个抽象类的基础上的 Java中的流主要有字节流和字符流 11 4 1流的类型 根据流动方向的不同 流分为输入流和输出流 对于输入和输出流 由于传输格式的不同 又分为字节流和字符流 字节流是指8位的通用字节流 以字节为基本单位 在java io包中 对于字节流进行操作的类大部分继承于InputStream 输入字节流 类和OutputStream 输出字节流 类 字符流是指16位的Unicode字符流 以字符 两个字节 为基本单位 非常适合处理字符串和文本 对于字符流进行操作的类大部分继承于Reader 读取流 类和Writer 写出流 类 11 4 2字节输入输出流层次 11 4 3字符输入输出流层次 11 5字节流 不管是输入流还是输出流 用完以后都必须关闭 且应放在finally子句中 读写操作都会抛异常 必须进行处理 汉字最好用字符流操作 11 5 1文件输入 输出流 importjava io classStreamTest publicstaticvoidmain String args throwsIOException FileInputStreamfis newFileInputStream 1 txt byte buf newbyte 1024 intlen fis read buf System out println newString buf 0 len fis close FileOutputStreamfos newFileOutputStream 1 txt fos write aaj akajaa pvzoi getBytes fos close 其构造方法参数中的字符串可以是路径也可以是文件名 11 5 2连接字节输入流 importjava io publicclassSequenceTest publicstaticvoidmain String args throwsIOException FileInputStreamfis1 newFileInputStream 1 txt FileInputStreamfis2 newFileInputStream 2 txt SequenceInputStreamsis newSequenceInputStream fis2 fis1 byte buf newbyte 1024 intlen1 sis read buf intlen2 sis read buf len1 1024 len1 System out println newString buf 0 len1 len2 sis close 当有多个流连接在一起时 关闭最外层流就可以了 11 4 6过滤字节流 它本身不是流 是一种用于扩展底层流功能的装饰器 它的子类 分别用来扩展字节流的某一种功能 它必须先创建对应字节流对象 再以它为参数来创建过滤字节流类的象 然后利用此对象扩展字节流的功能 就像把字节流包覆于其中 关闭文件时 一般只关闭外层流即可 11 4 6 1缓冲输入 输出流 BufferedInputStream的特点是 不断的从底层流中读取数据 可以减少I O请求次数 提高效率 BufferedInputStream的特点是 先将数据写到缓冲里 缓冲满后再一次性写入 可以减少I O请求次数 提高效率 一般在关闭前调用flush方法 将数据强制写入 importjava io classStreamTest publicstaticvoidmain String args throwsIOException FileInputStreamfis newFileInputStream 1 txt BufferedInputStreambis newBufferedInputStream fis byte buf newbyte 1024 intlen bis read buf System out println newString buf 0 100 bis close FileOutputStreamfos newFileOutputStream 1 txt BufferedOutputStreambos newBufferedOutputStream fos bos write aaj akajaa pvzoi getBytes bos flush bos close 11 4 6 2格式化输出流 PrintStream类的特点是 可以直接写基本数据类型和字符串 可指定自动刷新缓存 方法不会抛出异常标准类库中System等类都使用了它 importjava io classStreamTest publicstaticvoidmain String args throwsIOException FileOutputStreamfos newFileOutputStream 1 txt BufferedOutputStreambos newBufferedOutputStream fos PrintStreamps newPrintStream fos true ps print aaj akajaa pvzoi ps close 11 4 6 3数据输入 输出流 DataInput接口抽象出来的行为是 从二进制流中读取字节 并根据Java基本数据类型进行重构 DataOutput接口抽象出来的行为是 将数据从任意Java基本数据类型转换为一系列字节 并将这些字节写入字节流 DataInputStream和DataOutputStream分别实现了这两个接口 DataOutputStream的特点是 可以写基本数据类型 DataInputStream的特点是 可以读基本数据类型 读时文件到尾不会返回 1 而是抛出异常 因此 这两种流通常成对使用 否则容易发生异常 用这两种流时 读写的顺序应相同 否则读出的数据不对 importjava io classStreamTest publicstaticvoidmain String args throwsIOException FileOutputStreamfos newFileOutputStream 1 txt BufferedOutputStreambos newBufferedOutputStream fos DataOutputStreamdos newDataOutputStream bos byteb 3 inti 78 charch a floatf 4 5f dos writeByte b dos writeInt i dos writeChar ch dos writeFloat f dos flush dos close FileInputStreamfis newFileInputStream 1 txt BufferedInputStreambis newBufferedInputStream fis DataInputStreamdis newDataInputStream bis System out println dis readByte System out println dis readInt System out println dis readChar System out println dis readFloat dis close 11 4 6 11自定义流 importjava io classMyOutputStreamextendsFilterOutputStream MyOutputStream OutputStreamout super out publicvoidwrite intx throwsIOException super write x 10 自定义输出流时 一般只覆盖一个write方法 write intx 因为在内部 别的write方法都调用它 注意 方法要抛出异常 importjava io classMyInputStreamextendsFilterInputStream MyInputStream InputStreamin super in publicintread throwsIOException intx super read return x 1 1 x 10 publicintread byte b intoffset intlen throwsIOException intx super read b offset len for inti offset i offset x i b i byte b i 10 returnx 自定义输入流时需要覆盖两个方法 read 和read byte b intoffset intlen 测试自定义流 importjava io classStreamTest publicstaticvoidmain String args throwsIOException FileOutputStreamfos newFileOutputStream 1 txt MyOutputStreammos newMyOutputStream fos mos write aaj akajaa pvzoi getBytes mos flush mos close FileInputStreamfis newFileInputStream 1 txt MyInputStreammis newMyInputStream fis byte buf newbyte 1024 intlen mis read buf System out println newString buf 0 100 mis close 11 4 7对象序列化 对象序列化就是将对象某一时刻的状态保存到外部的存储介质上 对象序列化的功能 可以将程序运行时的某个状态保存到外部存储介质上 可以很好的完成对象深克隆 将序列化的对象读取出来叫反序列化 注意接口中方法抛出的异常 序列化时 一般扩展FileOutputStream和FileInputStream 11 4 7 5对象序列化的实现方法 importjava io classEmployeeimplementsSerializable Stringname intage doublesalary Employee Stringname intage doublesalary this name name this age age this salary salary 最简单的实现方法 声明实现Serializable接口 该接口是标记性接口 没有任何方法 只声明就可以了 这种方式实现的序列化时默认序列化行为 classObjectSerialTest publicstaticvoidmain String args throwsIOException ClassNotFoundException Employeee1 newEmployee zhangsan 25 3000 Employeee2 newEmployee lisi 24 3200 Employeee3 newEmployee wangwu 27 3800 FileOutputStreamfos newFileOutputStream employee txt ObjectOutputStreamoos newObjectOutputStream fos oos writeObject e1 oos writeObject e2 oos writeObject e3 oos close FileInputStreamfis newFileInputStream employee txt ObjectInputStreamois newObjectInputStream fis Employeee for inti 0 i 3 i e Employee ois readObject System out println e name e age e salary ois close 虽然Serializable接口中没有任何方法 但必须声明实现它才能序列化 否则运行时会抛异常反序列化的read方法返回的是Object的对象 需要强转为当前类的对象 子类继承父类时 如果父类没有实现序列化 而子类实现了 那么反序列化时要求父类有无参构造方法 子类继承父类时 如果父类实现序列化 而子类没有实现 则均可序列化 可持久化的数据才能序列化 不能序列化或不想序列化的数据可以用transient声明 二 在要实现序列化的类中提供两个私有方法根据需要给出自己想要的序列化方法 序列化和反序列化时会分别调用它们 privatevoidwriteObject ObjectOutputStreamoos throwsIOException oos writeInt age oos writeObject name privatevoidreadObject ObjectInputStreamois throwsIOException ClassNotFoundException age ois readInt name String ois readObject 三 实现Externalizable接口 importjava io classEmployeeimplementsExternalizable Stringname intage doublesalary publicEmployee Employee Stringname intage doublesalary this name name this age age this salary salary publicvoidwriteExternal ObjectOutputoo throwsIOException oo writeInt age oo writeObject name publicvoidreadExternal ObjectInputoi throwsIOException ClassNotFoundException age oi readInt name String oi readObject 该接口接中的两个方法 实参可传实现了ObjectInput和ObjectOutput接口的类的对象 因此ObjectInputStream和ObjectOutputStream的方法均可在它们中使用 类中必须有公有的无参构造方法 Serializable接口序列化一个对象时 有关类的信息 比如它的属性和这些属性的类型 都与实例数据一起被存储起来 在选择走Externalizable这条路时 Java只存储有关每个被存储类型的非常少的信息 如果两个接口都声明实现了 系统选择走Externalizable这条路 11 6字符流 字符流 一次I O请求读取一个字符 可用于读写纯文本文件 字符流都带同步锁 字符流的最底层实际就是字节流 它们之间的转换用桥接器 常用的四个桥接器 InputStreamReader OutputStreamWriter FileReader FileWriter 11 6 1字符输入 输出流 importjava io classStreamTest publicstaticvoidmain String args throwsIOException FileReaderfr newFileReader 2 txt char ch newchar 1024 intlen fr read ch System out println newString ch 0 len fr close FileWriterfw newFileWriter 2 txt fw write haoanoz我们 n zoaljva fw close 11 6 2缓存字符输入 输出流 importjava io classStreamTest publicstaticvoidmain String args throwsIOException FileReaderfr newFileReader 2 txt BufferedReaderbr newBufferedReader fr Stringstr while str br readLine null System out println str br close FileWriterfw newFileWriter 2 txt PrintWriterpw newPrintWriter fw true pw println haoanoz他们 n zoaljva pw close 在使用BufferedReader读文件时 一般用它的readLine方法 一般用PrintWriter代替BufferedWriter PrintWriter可以格式化输出 更灵活 且启用自动刷新后 println方法可以自动刷新缓存 文件复制 importjava io classStreamTest publicstaticvoidmain String args throwsIOException FileReaderfr newFileReader 2 txt BufferedReaderbr newBufferedReader fr FileWriterfw newFileWriter 1 txt PrintWriterpw newPrintWriter fw true Stringstr while str br readLine null pw println str br close pw close 文件读写GUI程序 importjava io importjava awt importja
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 商业银行金融科技人才普惠金融能力培养策略报告2025
- 2025年公众参与视角下环境影响评价公众满意度调查报告
- 电梯委托监督检验协议书
- 江苏国际货运代理协议书
- 珠宝厂出租转让合同范本
- 电梯井道施工安全协议书
- 防火隔热服采购合同范本
- 混凝土合同三方协议模板
- 领取小区大门钥匙协议书
- 私人仓库房屋租赁协议书
- 滦州事业单位笔试真题及答案2024
- 了解DSA-200型受电弓结构及作用讲解
- 煤矿开掘技术操作规程
- 《光伏组件功率衰减检验技术规范(征求意见稿)》
- 威图电柜空调SK3304500使用说书
- 客运驾驶员汛期安全培训
- 2023年湖北宜昌高新区社区专职工作人员(网格员)招聘考试真题及答案
- 【1例心肌梗塞患者的PCI术后护理探究7800字(论文)】
- 干部基本信息审核认定表
- 采购管理中的创新与持续改进
- 湖南省永州冷水滩区2021-2022学年七年级下学期期末语文试题答案
评论
0/150
提交评论