输入字节流 InputStream
InputStream 是所有输入字节流的父类, 它是一个抽象类
ByteArrayInputStreamStringBufferInputStreamFileInputStream 是三种基本的介质流, 它们分别从 Byte 数组 StringBuffer 和本地文件读取数据
PipedInputStream 是从与其他线程共用的管道中读取数据
ObjectInputStream 和所有的 FilterInputStream 的子类都是装饰流(装饰器模式的主角)
输出字节流 OutputStream
OutputStream 是所有输出字节流的父类, 它是一个抽象类
ByteArrayOutputStreamFileOutputStream 是两种基本的介质流, 它们分别向 Byte 数组和本地文件写入数据
PipedInputStream 是向与其他线程共用的管道中写入数据
ObjectInputStream 和所有的 FilterInputStream 的子类都是装饰流
总结
输入流 InputStream 或者 Reader: 从文件中读取到程序中;
输出流 OutputStream 或者 Writer: 从程序中输出到文件中
节点流
节点流: 直接与数据源相连, 读入或读出
直接使用节点流, 读写不方便, 为了更快的读写文件, 才有了处理流
常用的节点流
父类 :InputStream OutputStream Reader Writer
文件 :FileInputStream FileOutputStrean FileReader FileWriter 文件进行处理的节点流
数组 :
- ByteArrayInputStream
- ByteArrayOutputStream
CharArrayReader CharArrayWriter 对数组进行处理的节点流(对应的不再是文件, 而是内存中的一个数组)
字符串 :StringReader StringWriter 对字符串进行处理的节点流
管道 :PipedInputStream PipedOutputStream PipedReader PipedWriter 对管道进行处理的节点流
处理流
处理流和节点流一块使用, 在节点流的基础上, 再套接一层, 套接在节点流上的就是处理流如 BufferedReader. 处理流的构造方法总是要带一个其他的流对象做参数一个流对象经过其他流的多次包装, 称为流的链接
常用的处理流
缓冲流: BufferedInputStrean
BufferedOutputStream
BufferedReader BufferedWriter 增加缓冲功能, 避免频繁读写硬盘
转换流: InputStreamReader OutputStreamReader 实现字节流和字符流之间的转换
数据流: DataInputStream DataOutputStream 等 - 提供将基础数据类型写入到文件中, 或者读取出来
转换流
InputStreamReader OutputStreamWriter 要 InputStream 或 OutputStream 作为参数, 实现从字节流到字符流的转换
构造函数
- InputStreamReader(InputStream); // 通过构造函数初始化, 使用的是本系统默认的编码表 GBK
- InputStreamWriter(InputStream,String charSet); // 通过该构造函数初始化, 可以指定编码表
- OutputStreamWriter(OutputStream); // 通过该构造函数初始化, 使用的是本系统默认的编码表 GBK
- OutputStreamwriter(OutputStream,String charSet); // 通过该构造函数初始化, 可以指定编码表
实战演练
FileInputStream 类的使用: 读取文件内容
- package com.app;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- public class A1 {
- public static void main(String[] args) {
- A1 a1 = new A1();
- // 电脑 d 盘中的 abc.txt 文档
- String filePath = "D:/abc.txt" ;
- String reslut = a1.readFile( filePath ) ;
- System.out.println( reslut );
- }
- /**
- * 读取指定文件的内容
- * @param filePath : 文件的路径
- * @return 返回的结果
- */
- public String readFile( String filePath ){
- FileInputStream fis=null;
- String result = "" ;
- try {
- // 根据 path 路径实例化一个输入流的对象
- fis = new FileInputStream( filePath );
- //2. 返回这个输入流中可以被读的剩下的 bytes 字节的估计值;
- int size = fis.available() ;
- //3. 根据输入流中的字节数创建 byte 数组;
- byte[] array = new byte[size];
- //4. 把数据读取到数组中;
- fis.read( array ) ;
- //5. 根据获取到的 Byte 数组新建一个字符串, 然后输出;
- result = new String(array);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }catch (IOException e) {
- e.printStackTrace();
- }finally{
- if ( fis != null) {
- try {
- fis.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- return result ;
- }
- }
FileOutputStream 类的使用: 将内容写入文件
- package com.app;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- public class A2 {
- public static void main(String[] args) {
- A2 a2 = new A2();
- // 电脑 d 盘中的 abc.txt 文档
- String filePath = "D:/abc.txt" ;
- // 要写入的内容
- String content = "今天是 2017/1/9, 天气很好" ;
- a2.writeFile( filePath , content ) ;
- }
- /**
- * 根据文件路径创建输出流
- * @param filePath : 文件的路径
- * @param content : 需要写入的内容
- */
- public void writeFile( String filePath , String content ){
- FileOutputStream fos = null ;
- try {
- //1 根据文件路径创建输出流
- fos = new FileOutputStream( filePath );
- //2 把 string 转换为 byte 数组;
- byte[] array = content.getBytes() ;
- //3 把 byte 数组输出;
- fos.write( array );
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }catch (IOException e) {
- e.printStackTrace();
- }finally{
- if ( fos != null) {
- try {
- fos.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
注意:
在实际的项目中, 所有的 IO 操作都应该放到子线程中操作, 避免堵住主线程
FileInputStream 在读取文件内容的时候, 我们传入文件的路径 ("D:/abc.txt"), 如果这个路径下的文件不存在, 那么在执行 readFile() 方法时会报
FileNotFoundException
异常
FileOutputStream 在写入文件的时候, 我们传入文件的路径 ("D:/abc.txt"), 如果这个路径下的文件不存在, 那么在执行 writeFile() 方法时, 会默认给我们创建一个新的文件还有重要的一点, 不会报异常
效果图:
综合练习, 实现复制文件, 从 D 盘复制到 E 盘
- package com.app;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- public class A3 {
- public static void main(String[] args) {
- A3 a2 = new A3();
- // 电脑 d 盘中的 cat.png 图片的路径
- String filePath1 = "D:/cat.png" ;
- // 电脑 e 盘中的 cat.png 图片的路径
- String filePath2 = "E:/cat.png" ;
- // 复制文件
- a2.copyFile( filePath1 , filePath2 );
- }
- /**
- * 文件复制
- * @param filePath_old : 需要复制文件的路径
- * @param filePath_new : 复制文件存放的路径
- */
- public void copyFile( String filePath_old , String filePath_new){
- FileInputStream fis=null ;
- FileOutputStream fout = null ;
- try {
- // 根据 path 路径实例化一个输入流的对象
- fis = new FileInputStream( filePath_old );
- //2. 返回这个输入流中可以被读的剩下的 bytes 字节的估计值;
- int size = fis.available() ;
- //3. 根据输入流中的字节数创建 byte 数组;
- byte[] array = new byte[size];
- //4. 把数据读取到数组中;
- fis.read( array ) ;
- //5 根据文件路径创建输出流
- fout = new FileOutputStream( filePath_new ) ;
- //5 把 byte 数组输出;
- fout.write( array );
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }catch (IOException e) {
- e.printStackTrace();
- }finally{
- if ( fis != null) {
- try {
- fis.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if ( fout != null ) {
- try {
- fout.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
来源: http://www.bubuko.com/infodetail-2513563.html