IO流(3)—字节流

  1. IO体系:
    • 抽象基类————节点流(文件流)
    • InputStream——FileInputStream(字节流)
    • OutputStream——FileOutputSteam(字节流)
    • Reader ————FileReader(字符流)
    • Writer————FileWriter(字符流)
  2. 分类:
    • 按操作数据单位不同:字节流(8bit)主要处理除了文本文件以外的问文件、字符流(16bit)主要处理文本文件
    • 按数据流的流向不同:输入流、输出流
    • 按流的角色不同:节点流(直接作用于文件的:FileInputStream、FileOutputSteam、FileReader、FileWriter)、
    • 处理流(除了以上四个之外都是)

字节流:
这里介绍FileInputStream和FileOutputStream,用于处理视频文件、音频文件、图片、.doc。

示例代码:

public class FileInputOutputStreamTest {
    //使用FileInputStream硬盘读取一个文件,控制台打印
    @Test
    public void testFileInputStream(){
        //方法:read()
        //1.创建一个File类对象 
        File file = new File("E:/workspace/hello.txt");
        //2.创建一个FileInputStream类对象
        try {
            FileInputStream fis = new FileInputStream(file);
            try {
                //3.调用FileInputStream类的方法读取数据
                int i = fis.read();//read()会报异常,需要try catch
                while(i != -1){
                    System.out.print((char)i);
                    i = fis.read();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }finally{//无论前面出不出异常,这个是一定要执行的
                try {//因为执行该语句本身会报异常,所以try-catch
                    //4.关闭流
                    fis.close();
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
            }
        } catch (FileNotFoundException e) {
            System.out.println("指定文件不存在!");
        }
    }
    //方法read(byte[] b)从 硬盘读取字符,控制台打印
    @Test
    public void testFileInputStream1(){
        //1.创建一个File类对象 
        //2.创建一个FileInputStream类对象
        FileInputStream fis = null;
            try {
                File file = new File("E:/workspace/hello.txt");
                fis = new FileInputStream(file);
                //3.调用FileInputStream类的方法读取数据
                byte[] bytes = new byte[5];
                int len;//每次都入到数组中的长度   
                while((len = fis.read(bytes)) != -1){
                    for(int i  = 0; i < len; i++){
                        System.out.print((char)bytes[i]);
                    }
                    //或者这样写
                    //String str = new String(bytes,0,len);
                    //System.out.println(str);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }finally{
                if(fis != null){
                    try {//因为执行该语句本身会报异常,所以try-catch
                        //4.关闭流
                        fis.close();
                    } catch (Exception e2) {
                        e2.printStackTrace();
                    }
                }
            }
        }
    //使用FileOutputStream写出数据到硬盘一个文件
    @Test
    public void testFileOutputStream(){
        //1.创建一个file对象
        File file = new File("file/hello.txt");//当这个文件不存在时,就创建
        //2.创建一个输出流(FileOutputStream)对象,将file对象作为形参传递进给输出流(FileOutputStream)构造器中去
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(file);
            //3.写入操作
            fos.write(new String("I LOVE MY FIMARY 我爱我家!").getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            if(fos != null){
                try {
                    //4.关闭操作
                    fos.close();
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
            }
        }
    }
    //一边读取一边写入,把1.jpg文件中的内容复制到2.jpg文件中
    @Test
    public void testFileInputOutputStream(){
        //1.两个file对象
        File file1 = new File("file/1.jpg");
        File file2 = new File("file/2.jpg");
        //2.输入输出流
        FileInputStream fis = null;
        FileOutputStream fos = null;
        byte[] b = new byte[20];
        int len;
        try {
            fis = new FileInputStream(file1);
            fos = new FileOutputStream(file2);
            //3.开始循环读取1.jpg文件中的数据
            while((len = fis.read(b)) != -1){
                //4.读取之后就写入到2.jpg文件中
                fos.write(b,0,len);//错误的写法:fos.write(b)和fos.write(b,0,b.length)
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            if(fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(fos != null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    //使用FIleInputStream和FileOutputStream方法复制文件
    public void copyFile(String src,String dest){
        //1.两个file对象
        File file1 = new File(src);
        File file2 = new File(dest);
        //2.输入输出流
        FileInputStream fis = null;
        FileOutputStream fos = null;
        byte[] b = new byte[1024];
        int len;
        try {
            fis = new FileInputStream(file1);
            fos = new FileOutputStream(file2);
            //3.开始循环读取文件中的数据
            while((len = fis.read(b)) != -1){
                //4.读取之后就写入到另一个文件中
                fos.write(b,0,len);//错误的写法:fos.write(b)和fos.write(b,0,b.length)
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            if(fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(fos != null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    //执行以上方法,计算使用FIleInputStream和FileOutputStream传输文件使用的时间
    @Test
    public void testCopyFile(){
        long start = System.currentTimeMillis();
        String src = "file/1.mp4";
        String dest = "file/3.mp4";
        copyFile(src,dest);
        long end = System.currentTimeMillis();
        System.out.println("花费时间:"+(end - start));//花费时间为1085毫秒
    }
}
原文地址:https://www.cnblogs.com/tengpengfei/p/10454006.html