关于IO的一些总结与体会

常用的3种,其中抽象基类和访问文件属于节点流,缓冲流属于处理流
IO流是用来处理设备与程序之间的数据传输。
按流向分为:
输入流:OutputStream和Writer为基类。
输出流:InputStream和Reader为基类。
按处理的单位分为:
字节流:InputStream基类。
字符流:OutputStream基类。
按流的角色分为:
节点流:可以从一个特定的 IO 设备读/写数据的流(访问文件相关,也叫文件流,程序直接作用在文件上)。
处理流:对一个已存在的流进行连接和封装,通过封装后的流来实现数据读/写操作。
一、在访问文件使用FileInputStreamOutputStream时的步骤为:
1.创建一个File类的对象;
2.创建一个FileInputStream类或FileOutputStream类的对象;
3.调用FileInputStream或FileOutputStream中的方法,实现file文件的读取或写入;
4.关闭相应的流(遵循先开后关的准则)。

package com.hpe.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
/**
 * 类描述:测试输入流
 * 作者: author  
 * 创建日期:2018年8月15日
 * 修改人:
 * 修改日期:
 * 修改内容:
 * 版本号: 1.0.0
 */
public class TestFileInputStream {
    public static void main(String[] args) {
        FileInputStream fis=null;
        try {
            File file=new File("hello.txt");
            fis=new FileInputStream(file);
            // 将读取到的数据写入数组  
            byte[] b=new byte[5];
            // 每次读入到byte中字节的长度
            int len;
            while ((len=fis.read(b))!=-1) {
                for (int i = 0; i <len; i++) {
                    // 由于byte中存储的是该字符在ASCII代码中对应的十进制值
                    // 因此需要转换成char才能将字符显示出来
                    System.out.print((char)b[i]);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (fis!=null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
package com.hpe.test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 类描述:测试输出流
 * 作者: author  
 * 创建日期:2018年8月15日
 * 修改人:
 * 修改日期:
 * 修改内容:
 * 版本号: 1.0.0
 */
public class TestFileOutputStream {
    public static void main(String[] args) {
        // 1.创建一个File对象,指定要写入的文件位置
        FileOutputStream fos = null;
        try {
            File file = new File("hello2.txt");
            // 2.创建一个FileOutputStream对象,将file的对象作为形参传递给构造器中
            fos = new FileOutputStream(file);
            // 3.写入操作将字符串转为字节数组
            String str = new String("hello");
            fos.write(str.getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 4.关闭输出流
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

二、使用BuffererInputStreamBufferedOutputStream时的步骤:
1.提供读入、写出的文件;
2.创建响应节点流FileInputStream FileOutputStream;
3.将创建的节点流的对象作为形参传递给缓冲流的构造器中;
4.调用BuffererInputStream的 read()方法和BufferedOutputStream的write()方法;
5.关闭相应的流(遵循先开后关的准则)。

package com.hpe.test;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
 * 类描述:测试缓冲流
 * 作者: author  
 * 创建日期:2018年8月15日
 * 修改人:
 * 修改日期:
 * 修改内容:
 * 版本号: 1.0.0
 */
public class TestBufferedInputOutputStream {
    public static void main(String[] args) {
        // 使用BuffererInputStream和BufferedOutputStream
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            // 1.提供读入、写出的文件
            File file1 = new File("1.jpg");
            File file2 = new File("3.jpg");
            // 2.创建响应节点流FileInputStream FileOutputStream
            FileInputStream fis = new FileInputStream(file1);
            FileOutputStream fos = new FileOutputStream(file2);
            // 3.将创建的节点流的对象作为形参传递给缓冲流的构造器中
            bis = new BufferedInputStream(fis);
            bos = new BufferedOutputStream(fos);
            // 4.实现文件复制
            byte[] b = new byte[1024];
            int len;
            // 读取文件
            while ((len = bis.read(b)) != -1) {
                // 写入文件
                bos.write(b, 0, len);
                // 刷新数据
                bos.flush();
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        } finally {
            // 5.关闭流
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}

1).对于非文本文件只能使用字节流进行处理。
2).要读取的文件必须得存在,写入的文件可以不存在(不存在的话会自动创建)。
3).缓冲流可以提高文件操作的效率。

猜你喜欢

转载自blog.csdn.net/qq_42825815/article/details/81710308
今日推荐