IO流之字节流

1.字节流

字节输出流

  close()释放资源

  flush()刷新数据

  write()把数据写入文件

2.FileOutputStream类

构造方法

FileOutputStream(路径,是否续写)

3.IO异常的处理

public class FileOutputStreamDemo3 {
    public static void main(String[] args) {
        File file = new File("c:\\file.txt");
        //定义FileOutputStream的引用
        FileOutputStream fos = null;
        try {
            //创建FileOutputStream对象
            fos = new FileOutputStream(file);
            //写出数据
            fos.write("abcde".getBytes());
        } catch (IOException e) {
            System.out.println(e.toString() + "----");
throw new RuntimeException("文件写入失败,重试");

        } finally {
            //一定要判断fos是否为null,只有不为null时,才可以关闭资源
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    throw new RuntimeException("关闭资源失败");
                }
            }
        }
    }
}

4.字节输入流FileInputStream

常用方法:read()读取文件

5.字节流复制文件

  1.一个字节复制

  2.一个字节数组复制

public static void main(String[] args) throws IOException {
        //复制一张图片
        //数据源
        FileInputStream fis=new FileInputStream("/Users/air/io0803/timg.jpeg");
        //目的地
        FileOutputStream fos=new FileOutputStream("/Users/air/io0803/a/timg1.jpeg");
        Date date=new Date();
        
        int len=0;
        while((len=fis.read())!=-1) {
            fos.write(len);
        }
        Date date1=new Date();
        System.out.println(date1.getTime()-date.getTime());
        fis.close();
        fos.close();
    }
public static void main(String[] args) throws IOException {
        //复制图片
        //数据源
        FileInputStream fis=new FileInputStream("/Users/air/io0803/timg.jpeg");
        //目的地
        FileOutputStream fos=new FileOutputStream("/Users/air/io0803/a/timg2.jpeg");
        Date date=new Date();
        byte[] bytes=new byte[1024];
        int len=0;
        while((len=fis.read(bytes))!=-1) {
            fos.write(bytes,0,len);
        }
        Date date1=new Date();
        System.out.println(date1.getTime()-date.getTime());
        fis.close();
        fos.close();
    }

2.字符流

字符输入流FileReader

常用方法:read()读取数据

构造方法:FileReader(路径)

字符输出流FileWriter

常用方法:write()写入数据;flush()刷新

构造方法:FileWriter(路径)

public static void main(String[] args) throws IOException {
        // 数据源
        FileReader fr = new FileReader("/Users/air/io0803/demo04.txt");
        // 数据源
        FileReader fr1 = new FileReader("/Users/air/io0803/demo04.txt");
        // 目的地
        FileWriter fw = new FileWriter("/Users/air/io0803/a/demo12.txt");
        // 目的地
        FileWriter fw1 = new FileWriter("/Users/air/io0803/a/demo13.txt");
        copy(fr, fw);
        copy(fr1, fw1);
    }

    // 字符复制
    public static void copy(FileReader fr, FileWriter fw) throws IOException {
        Date date = new Date();
        int len = 0;
        while ((len = fr.read()) != -1) {
            fw.write(len);
        }
        Date date1 = new Date();
        System.out.println(date1.getTime() - date.getTime());
        fr.close();
        fw.close();
    }

    // 字符数组复制
    public static void copy1(FileReader fr, FileWriter fw) throws IOException {
        Date date = new Date();
        char[] ch = new char[1024];
        int len = 0;
        while ((len = fr.read(ch)) != -1) {
            fw.write(ch, 0, len);
            fw.flush();
        }
        Date date1 = new Date();
        System.out.println(date1.getTime() - date.getTime());
        fr.close();
        fw.close();
    }

 

posted @ 2020-11-02 21:28  杰斯-java  阅读(81)  评论(0)    收藏  举报