第八章-io技术_第1页
第八章-io技术_第2页
第八章-io技术_第3页
第八章-io技术_第4页
第八章-io技术_第5页
已阅读5页,还剩13页未读 继续免费阅读

下载本文档

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

文档简介

Comment 木木木木1 这个特点 我们 在介绍方法的用法时 尤其注意 引言引言 1 数据源数据源 data source 提供原始数据提供原始数据的原始媒介 常见的 数据库 文件 其他程序 内存 网 络连接 IO 设备 2 流的概念流的概念 Stream 名词 水流 趋势 动词 流出 流动 数据源就像水箱 流就像水管中流着的水流 程序就是我们数据源就像水箱 流就像水管中流着的水流 程序就是我们 最终的用户 最终的用户 流是一个抽象 动态的概念 是一连串连续动态 的数据集合 3 第一个简单的 IO 流程序 将文件中的数据读入 当程序需要读取数据源的数据时 就会通过就会通过 IO 流对象开启一个通向数据源的流 流对象开启一个通向数据源的流 通过这个 IO 流对象相关方法可以顺序读取顺序读取流中的数据 同理 通过流向目的地写入数 据通过流来处理 基本代码如下 这个代码是通过流对象从一个文件中读取数据 这个代码是通过流对象从一个文件中读取数据 try FileInputStream fis new FileInputStream d a txt 内容是 abc int s1 fis read 97 int s2 fis read 98 int s3 fis read 99 int s4 fis read 1 int s5 fis read 1 System out println s1 fis close catch FileNotFoundException e e printStackTrace catch IOException e e printStackTrace 上面的代码可以升级为如下更加标准的代码 上面的代码可以升级为如下更加标准的代码 FileInputStream fis null try fis new FileInputStream d a txt 内容是 abc Comment 木木木木2 也可以对其他处 理流 StringBuilder sb new StringBuilder int temp 0 while temp fis read 1 sb append char temp System out println sb catch FileNotFoundException e e printStackTrace catch IOException e e printStackTrace finally try if fis null fis close 流对象 使用完后必须关闭 catch IOException e e printStackTrace 4 Java 中流的概念细分 流的方向 输入流 数据源到程序 InputStream Reader 读进来 输出流 程序到目的地 OutPutStream Writer 写出去 处理数据单元 字节流 按照字节读取数据 InputStream OutputStream 字符流 按照字符读取数据 Reader Writer 功能不同 节点流 可以直接从数据源或目的地读写数据 处理流 不直接连接到数据源或目的地 是处理流的流处理流的流 通过对其他流的处理提 高程序的性能 节点流和处理流的关系 节点流和处理流的关系 节点流处于 io 操作的第一线 所有操作必须通过他们进行 处理流可以对节点流 进行包装 提高性能或提高程序的灵活性 处理流 5 Java 中 IO 流类的体系 这里只列出常用的类 详情可以参考 JDK API 文档 红色粗体 标注为最常用 必须掌握的 四个基本抽象类 四个基本抽象类 InputStream Object File RandomAccessFile InputStream OutputStream Reader Writer ByteArrayInputStream SequenceInputStream StringBufferInputStream PipedInputStream FileInputStream FilterInputStream ObjectInputStream BufferedInputStream PushBackInputStream LineNumberInputStream ByteArrayOutputStream PipedOutputStream FileOutputStream FilterOutputStream ObjectOutputStream BufferedOutputStream PrintStream BufferedReader FilterReader InutStreamReader PipedReader StringReader FileReader BufferedWriter FilterWriter OututStreamWriter PipedWriter StringWriter FileWriter DateOutput DateInput Comment 木木木木3 也就是说 处理 文本的话使用字符流 此抽象类是表示字节输入流的所有类的超类 InputSteam 是一个抽象类 它不可以实 例化 数据的读取需要由它的子类来实现 根据节点的不同 它派生了不同的节点流子类 继承自 InputSteam 的流都是用于向程序中输入数据 且数据的单位为字节 8 bit 下图中深色为节点流 浅色为处理流 常用方法 read close 详情可以参考 API 文档 OutputStream 此抽象类是表示输出字节流的所有类的超类 输出流接受输出字节并将这些字节发送 到某个接收器 常见子类的使用方法 1 FileInputStream 和 FileOutputStream 构造方法和常用方法 自己参考 API 文档 FileInputStream 用于读取诸如图像数据之类的原始字节流 要读取字符流 请考虑 使用 FileReader available 并不是指定文件还有多少数据没有读 不然 如果是 100g 的 大文件 返回得数就溢出了 不过 对于普通文件的话可以看做是剩余的未读数 据 返回下一次对此输入流调用的方法可以不受阻塞地从此输入流读取 或跳过 返回下一次对此输入流调用的方法可以不受阻塞地从此输入流读取 或跳过 的估计剩余字节数 的估计剩余字节数 FileOutputStream 用于写入诸如图像数据之类的原始字节的流 要写入字符流 请考虑使用 FileWriter 练习 文件读取到内存中 文件读取到内存中 void testInputStream FileInputStream fis null StringBuilder sb new StringBuilder int temp 0 int num 0 long date1 System currentTimeMillis try fis new FileInputStream d a txt 内容是 abc while temp fis read 1 sb append char temp num 使用数组作为缓存 读取的效率大大提高 byte buffer new byte 1024 while temp fis read buffer 1 sb append new String buffer 0 temp System out println sb catch FileNotFoundException e e printStackTrace catch IOException e e printStackTrace finally try fis close catch IOException e e printStackTrace new File d File separator a txt long date2 System currentTimeMillis System out println 耗时 date2 date1 将字符串 字节数组的内容写入到文件中 FileOutputStream fos null String string abcdefg try fos new FileOutputStream d a txt true fos write string getBytes catch FileNotFoundException e e printStackTrace catch IOException e TODO Auto generated catch block e printStackTrace finally try fos close catch IOException e e printStackTrace 实现文件的复制功能 实现文件的复制功能 copyFile d a txt d b txt b txt可以不存在 void copyFile String src String dec FileInputStream fis null FileOutputStream fos null byte buffer new byte 1024 为了提高效率 设置缓存数 组 int temp 0 try fis new FileInputStream src fos new FileOutputStream dec while temp fis read buffer 1 fos write buffer 0 temp catch FileNotFoundException e e printStackTrace catch IOException e e printStackTrace finally try fos close catch IOException e e printStackTrace try fis close catch IOException e e printStackTrace copy目录下面所有的内容到另一个目录 作业 目录下面所有的内容到另一个目录 作业 public class TestCopy public static void main String args 1 实现一个文件到另一个文件的实现一个文件到另一个文件的copy 2 实现一个目录实现一个目录 目录下面只有文件目录下面只有文件 到另一个目录的到另一个目录的copy 3 实现一个目录实现一个目录 目录下面包含文件和子目录目录下面包含文件和子目录 到另一个目录的到另一个目录的copy 4 利用递归 实现将一个目录下面所有的内容利用递归 实现将一个目录下面所有的内容copy到另一个目录到另一个目录 File file new File D share 0100 JavaSE ppt File file2 new File e tt copyDir file file2 负责负责copy一个目录下面所有的文件和子目录到另一个目录一个目录下面所有的文件和子目录到另一个目录 static void copyDir File file File file2 new File file2 file getName mkdir copyFile new File file 22 txt new File file2 22 txt File files file listFiles for File f files if f isDirectory copyDir f new File file2 file getName if f isFile copyFile f new File new File file2 file getName f getName 将将d a 下所有的内容下所有的内容copy到到e tt static void copyFile File src File dec FileInputStream fis null FileOutputStream fos null byte buffer new byte 1024 为了提高效率 设置缓存数为了提高效率 设置缓存数 组组 int temp 0 try fis new FileInputStream src fos new FileOutputStream dec while temp fis read buffer 1 fos write buffer 0 temp catch FileNotFoundException e e printStackTrace catch IOException e e printStackTrace finally try fos close catch IOException e e printStackTrace try fis close catch IOException e e printStackTrace public static void copy File src File desx new File desx src getName mkdir File files src listFiles if files length 0 copy1 src new File desx src getName else for File temp files if temp isDirectory false copy1 temp new File new File desx src getName temp getName else copy temp new File new File desx src getName temp getName public static void copy1 File src File desx FileInputStream inputStream null FileOutputStream outputStream null try inputStream new FileInputStream src outputStream new FileOutputStream desx byte temp new byte 1024 int index 0 while index inputStream read temp 1 outputStream write temp 0 index catch FileNotFoundException e TODO Auto generated catch block e printStackTrace catch IOException e TODO Auto generated catch block e printStackTrace 2 ByteArrayInutStream 和和 ByteArrayOutputStream 经常用在需要流和数组之间转化 经常用在需要流和数组之间转化 的情况 的情况 ByteArrayInputStream 包含一个内部缓冲区 该缓冲区包含从流中读取的字 节 内部计数器跟踪 read 方法要提供的下一个字节 说白了 FileInputStream 是把文件当做数据源 ByteArrayInputStream 则是把内存 中的某个数组当做数据源 ByteArrayOutputStreamByteArrayOutputStream 此类实现了一个输出流 其中的数据被写入一个 byte 数组 缓冲区会 随着数据的不断写入而自动增长 可使用 toByteArray 和 toString 获取数据 ByteArrayOutputStream 将一个输出流指向一 个 Byte 数组 但这个 Byte 数组是 ByteArrayOutputStream 内部内置的 不需要我们来定义 注 注 不需要关闭流的 但是调用 close 也没有问题 close 不做任何事情 因为 ByteArrayOutputStream 本身操作的是数组 并没有打开文件描述符之类的 所以 不需要关闭 简单测试简单测试ByteArrayInputStream ByteArrayInputStream bais null StringBuilder sb new StringBuilder int temp 0 int num 0 long date1 System currentTimeMillis try byte b abcdefghijklmnopqrstuvwxyz getBytes bais new ByteArrayInputStream b bais new ByteArrayInputStream b 2 10 while temp bais read 1 sb append char temp num System out println sb System out println 读取的字节数 num finally try bais close catch IOException e e printStackTrace new File d File separator a txt long date2 System currentTimeMillis System out println 耗时 date2 date1 将输入流的内容读入到一个数组中返回 将输入流的内容读入到一个数组中返回 byte getBytesFromIS FileInputStream fis ByteArrayOutputStream baos null int temp 0 try baos new ByteArrayOutputStream while temp fis read 1 baos write temp return baos toByteArray 从而返回一个包含输入流所有 内容的数组 catch IOException e e printStackTrace return null 3 BufferedInputStream 和 BufferedOutputStream 是处理流 通过内部缓存数组来提高操作流的效率 使用 BufferedInputStream 和 BufferedOutputStream 实现文件的复制 void copyFile String src String dec FileInputStream fis null BufferedInputStream bis null FileOutputStream fos null BufferedOutputStream bos null int temp 0 Comment s4 bufferedOutputStream 的 write 一定要强制刷新 吗 Comment s5 必须强制刷新 try fis new FileInputStream src fos new FileOutputStream dec bis new BufferedInputStream fis bos new BufferedOutputStream fos while temp bis read 1 bos write temp bos flush catch FileNotFoundException e e printStackTrace catch IOException e e printStackTrace finally 增加处理流后 注意流的关闭顺序 增加处理流后 注意流的关闭顺序 后开的先关闭 后开的先关闭 try bos close catch IOException e e printStackTrace try bis close catch IOException e e printStackTrace try fos close catch IOException e e printStackTrace try fis close catch IOException e e printStackTrace BufferedWiter 和 BufferedRead public static void copyBychar FileReader fileReader null BufferedReader bufferedReader null Comment s6 注意 先关闭高效类 再关闭基本类 由于高效率依存于基 本类 不需要强制刷新 FileWriter fileWriter null BufferedWriter bufferedWriter null try fileReader new FileReader new File d add text java bufferedReader new BufferedReader fileReader fileWriter new FileWriter new File d sss java bufferedWriter new BufferedWriter fileWriter String temp while temp bufferedReader readLine null bufferedWriter write temp catch FileNotFoundException e TODO Auto generated catch block e printStackTrace catch IOException e TODO Auto generated catch block e printStackTrace finally try bufferedReader close fileReader close bufferedWriter close fileWriter close catch IOException e TODO Auto generated catch block e printStackTrace 4 DataInputStream 和 DataOutputStream 数据流数据流通常在流中写入或读取一个结果时使用通常在流中写入或读取一个结果时使用 如网络数据传递如网络数据传递 DataInputStream 和 DataOutputStream 提供了可以存取与机器无关的所有提供了可以存取与机器无关的所有 Java 基础基础 类型数据 如 类型数据 如 int double 等 的方法等 的方法 public class TestDataStream public static void main String args DataOutputStream dos null DataInputStream dis null try dos new DataOutputStream new BufferedOutputStream new FileOutputStream D share 0100 JavaSE ppt src 8io data txt dis new DataInputStream new BufferedInputStream new FileInputStream D share 0100 JavaSE ppt src 8io data t xt dos writeDouble Math random dos writeBoolean true dos writeInt 10 dos writeChar a dos writeUTF 中國字 dos flush 从文件中直接读取数据 System out println double dis readDouble System out println boolean dis readBoolean System out println int dis readInt System out println char dis readChar System out println String dis readUTF catch IOException e e printStackTrace finally try dos close catch IOException e TODO Auto generated catch block e printStackTrace try dis close catch IOException e TODO Auto generated catch block e printStackTrace 5 ObjectInputStream 和和 ObjectOutputStream 见专题文档 java 对象的序列化和反序列化 6 PrintStream System out 就是一个 PrintStream 简单看看就行了 Reader 用于读取字符流的抽象类 数据单位为字符 Writer 写入字符流的抽象类 FileReader 和 FileWriter 实现文本文件的实现文本文件的 copy 写法和使用stream基本一样 只不过 读取时是读取的字符只不过 读取时是读取的字符 FileReader fr null FileWriter fw null int c 0 try fr new FileReader d a txt fw new FileWriter d d txt while c fr read 1 fw write c catch FileNotFoundException e e printStackTrace catch IOException e e printStackTrace finally try fw close catch IOException e e printStackTrace try fr close catch IOException e e printStackTrace BufferReader 和和 BufferWriter 实际开发中用的很多 需要大家亲自敲一遍 实际开发中用的很多 需要大家亲自敲一遍 使用他们来实现文本文件的复制 使用他们来实现文本文件的复制 FileReader fr null FileWriter fw null BufferedReader br null BufferedWriter bw null String tempString try fr new FileReader d a txt fw new FileWriter d d txt br new BufferedReader fr bw new BufferedWriter fw while tempString br readLine null bw write tempString catch FileNotFoundException e e printStackTrace catch IOException e e printStackTrace finally try bw close catch IOException e1 e1 printStackTrace try br close catch IOException e1 e1 printStackTrace try fw close catch IOException e e printStackTrace try fr close catch IOException e e printStackTrace InputStreamReader 和和 OutputStreamWriter 用来实现将字节流转化成字符流 接受用户的输入 BufferedReader bReader null try System out println 名字 bReader new BufferedReader new InputStreamReader System in System out println 用户输入 bReader readLine catch IOException e TODO Auto generated catch block e printStackTrace 附录 附录 IO 中其他常用类 中其他常用类 File 类 文件和目录路径名的抽象表示形式 一个 File 对象可以代表一个文件或目录 但是不 是完全对应的 建立 File 对象不会对文件系统产生影响 RandomAccessFile 不作为重点 了解即可 工作中用的非常少 不作为重点 了解即可 工作中用的非常少 还是不讲了 用的不多 用来任意读取文件中某个位置的信息 有兴趣 大家可以自 学一下 学习一下学习一下 Apache Commons 中的中的 IO 扩展类 了解一下 扩展类 了解一下 http commons apache org io 简单学习一下 不需要看具体的简单学习一下 不需要看具体的 api 如果能在 如果能在 google 上搜索到中文帮助 可以详细研究一下 上搜索到中文帮助 可以详细研究一下 JAVA 流技术总结流技术总结 作业 作业 要检查每个人写的 要检查每个人写的 字节流字节流数据源数据源 目的目的 地地 经常用于经常用于典型用法典型用法 字符流字符流数据源数据源 目的目的 地地 特点特点经常用于经常用于典型用法典型用法 3 班中南大学王鑫作业 字节流字节流类型类型数据源数据源 目的地目的地经常用于经常用于典型用法典型用法 InputStream字节输入流超类数据源 文件 内存 数

温馨提示

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

评论

0/150

提交评论