2021.6.11笔记 IO


IO概述(概念&分类)

在这里插入图片描述


字节输出流

OutputStream&FileOutputStream

在这里插入图片描述 1.写入数据到文件(FileOutputStream)

在这里插入图片描述

public class OutputStreamC {
    public static void main(String[] args) throws IOException {
        FileOutputStream ops = new FileOutputStream("F:\\test\\aa.txt");      //可以是相对路径(但是我不太敢用)
        ops.write(150);
        ops.close();
    }
}

文件存储的原理和记事本打开原理

在这里插入图片描述

2.写多个字节的方法

在这里插入图片描述

public class OutputStreamC {
    public static void main(String[] args) throws IOException {
        FileOutputStream ops = new FileOutputStream("F:\\test\\aa.txt");
        byte[] byteArr = {66, 67, 68, 69};
        ops.write(byteArr);
        ops.close();
    }
}public class OutputStreamC {
    public static void main(String[] args) throws IOException {
        FileOutputStream ops = new FileOutputStream("F:\\test\\aa.txt");
        byte[] byteArr = {66, 67, 68, 69};
        ops.write(byteArr);
        byte[] bytes = "你好".getBytes();
        System.out.println(Arrays.toString(bytes));
        ops.write(bytes);
        ops.close();
        ops.close();
    }
}

在这里插入图片描述

public class OutputStreamC {
    public static void main(String[] args) throws IOException {
        FileOutputStream ops = new FileOutputStream("F:\\test\\aa.txt");
        byte[] byteArr = {66, 67, 68, 69};
        ops.write(byteArr,1,2);
        ops.close();
    }
}

续写和换行

在这里插入图片描述

public class OutputStreamC {
    public static void main(String[] args) throws IOException {
        FileOutputStream ops = new FileOutputStream("F:\\test\\aa.txt",true);
        for (int i = 0; i < 10; i++) {
            ops.write("你好".getBytes());
            ops.write("\r\n".getBytes());
        }
    }
}

字节输入流

在这里插入图片描述

读取字节数据

在这里插入图片描述
在这里插入图片描述

public class InputStreamClass {
    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("F:\\test\\aa.txt");
        int len = 0;		//保存读取的数据,防止多次读取
        while((len = fis.read()) != -1) {
            System.out.println((char)len);
        }
        fis.close();
    }
}

在这里插入图片描述

一次读取一个字节的原理

在这里插入图片描述

一次读取多个字节的原理

在这里插入图片描述

在这里插入图片描述

public class NewDemo {
    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("F:\\test\\aa.txt");
        byte[] bytes = new byte[1024];
        int len = 0;
        while((len = fis.read(bytes)) != -1) {
            System.out.println(new String(bytes,0,len));
        }
    }
}

练习:文件复制

在这里插入图片描述
在这里插入图片描述

public class CopyDemo01 {
    public static void main(String[] args) throws IOException {
        long start = System.currentTimeMillis();
        FileInputStream fis = new FileInputStream("E:\\Copy1\\aa.txt");
        FileOutputStream fos = new FileOutputStream("F:\\Copy2\\cc.txt");
        /*int len = 0;
        while((len = fis.read()) != -1) {
            fos.write(len);
        }*/
        int len = 0;
        byte[] bytes = new byte[1024];
        while((len = fis.read(bytes)) != -1) {
            fos.write(bytes,0,len);
        }
        fos.close();
        fis.close();
        long finish = System.currentTimeMillis();
        System.out.println("总共执行了:" + (finish - start) + "毫秒");
    }
}

字符输入流

Reader类&FileReader类

在这里插入图片描述

public class FileReaderClass {
    public static void main(String[] args) throws IOException {
        FileReader fr = new FileReader("F:\\test\\aa.txt");
        int len = 0;
        /*while ((len = fr.read()) != -1) {
            System.out.print((char)len);
        }*/
        char[] chars = new char[1024];
        while ((len = fr.read(chars)) != -1) {
            System.out.print(new String(chars,0,len));
        }
        fr.close();
    }
}

字符输出流

Writer类&FileWriter类

在这里插入图片描述

public class FileWriterClass {
    public static void main(String[] args) throws IOException {
        FileWriter fw = new FileWriter("F:\\test\\aa.txt");
        fw.write(97);
        fw.flush();
        fw.close();     //close时如果没flush会自动把数据刷到文件中去,用flush跟close对比是,flush后仍然可以write,但是close后流关闭了,不可以再write
        //(原来的数据将被覆盖)
    }
}

写数据的其他方法

在这里插入图片描述

public class FileWriterC {
    public static void main(String[] args) throws IOException {
        FileWriter fw = new FileWriter("H:\\test\\aa.txt");
        char[] chars = {'a', 'b', 'c', 'd'};
        fw.write(chars);
        fw.write(chars,0,2);
        fw.write("小新妮妮风间");
        fw.write("小新妮妮风间",1,3);
        fw.close();
    }
}

续写和换行

在这里插入图片描述

public class FileWriterC {
    public static void main(String[] args) throws IOException {
        FileWriter fw = new FileWriter("H:\\test\\aa.txt",true);
        for (int i = 0; i < 10; i++) {
            fw.write("你好" + "\r\n");
        }
        fw.close();
    }
}

try…catch…finally处理流中的异常

在这里插入图片描述

public class NewClass {
    public static void main(String[] args) {
        FileWriter fw = null;
        for (int i = 0; i < 10; i++) {
            try {
                fw = new FileWriter("F:\\test\\aa.txt");
                fw.write("小新妮妮风间" + i + "\r\n");
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if(fw != null) {
                    try {
                        fw.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值