6 IO流

6 IO流

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VQYrTmNE-1677208938639)(images/cAS8MD4kCYNLn27m04TLSs8pYQCwuNs4uLaN4KKWfDg.png)]

  • 文本文件:txt, java, c, cpp 使用字符流操作
  • 非文本文件:jpg, mp3, mp4, doc, ppt 使用字节流操作
  • try-catch-finally处理异常方式:
    //这是一个main方法,是程序的入口:
    public static void main(String[] args)  {
        //1.有一个源文件
        File f1 = new File("d:\\Test.txt");
        //2.有一个目标文件:
        File f2 = new File("d:\\Demo.txt");
        //3.搞一个输入的管 怼到源文件上:
        FileReader fr = null;
        FileWriter fw = null;
        try {
            fr = new FileReader(f1);
            //4.搞一个输出的管,怼到目标文件上:
            fw = new FileWriter(f2);
            //5.开始动作:
            char[] ch = new char[5];
            int len = fr.read(ch);
            while(len!=-1){
                String s = new String(ch,0,len);
                fw.write(s);
                len = fr.read(ch);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //6.关闭流:(关闭流的时候,倒着关闭,后用先关)
            try {
                if(fw!=null){//防止空指针异常
                    fw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(fr!=null){
                    fr.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

7.1 字节流

7.1.1 FileInputStream / FileOuputStream

  1. 文件是utf-8进行存储的,所以英文字符底层实际占用1个字节。但是中文字符,底层实际占用3个字节
  2. 如果文件是文本文件,不推荐字节流,推荐字符流。
  3. read()读取一个字节,但是返回值是int类型,而不是byte类型
// 1 每次读单个字节
public static void main(String[] args) throws IOException {
    File f = new File("D:\\Test.txt");
    FileInputStream fis = new FileInputStream(f);

    int n = fis.read();
    while(n != -1){
        System.out.println(n);
        n = fis.read();
    }
    fis.close();
}

// 2 每次读一个缓冲数组
public static void main(String[] args) throws Exception {
    File f1 = new File("E:\\javaProject\\coding\\test\\test.png");
    File f2 = new File("E:\\javaProject\\coding\\test\\test1.png");
    FileInputStream fis = new FileInputStream(f1);
    FileOutputStream fos = new FileOutputStream(f2);
    // 利用缓冲数组:
    byte[] b = new byte[1024*8];
    int len = fis.read(b);
    while(len != -1){
        fos.write(b, 0, len);
        len = fis.read(b);
    }
    // 关闭流:(倒着关闭流,先用后关)
    fos.close();
    fis.close();
}

7.1.2 BufferedInputStream / BufferedOutputStream (缓冲字节流)

public static void main(String[] args) throws Exception {
    File f1 = new File("E:\\javaProject\\coding\\test\\test.png");
    File f2 = new File("E:\\javaProject\\coding\\test\\test1.png");
    FileInputStream fis = new FileInputStream(f1);
    FileOutputStream fos = new FileOutputStream(f2);
    BufferedInputStream bis = new BufferedInputStream(fis);
    BufferedOutputStream bos = new BufferedOutputStream(fos);

    byte[] b = new byte[1024*6];
    int len = bis.read(b);
    while(len!=-1){
        bos.write(b,0,len);
        len = bis.read(b);
    }
    // 关闭流:
    // 如果处理流包裹着节点流的话,那么其实只要关闭高级流(处理流),那么里面的字节流也会随之被关闭。
    bos.close();
    bis.close();
}

7.2 字符流

7.2.1 InputStreamReader / OutputStreamWriter (转换流)

作用:将字节流和字符流进行转换

public static void main(String[] args) throws IOException {
    File f = new File("E:\\javaProject\\coding\\test\\test.txt");
    FileInputStream fis = new FileInputStream(f);
    InputStreamReader isr = new InputStreamReader(fis, "utf-8");

    char[] ch = new char[20];
    int len = isr.read(ch);
    while(len!=-1){
        System.out.print(new String(ch,0,len));
        len = isr.read(ch);
    }
    // 关闭流
    isr.close();
}

7.2.2 BufferedReader / BufferedWriter (缓冲字符流)

public static void main(String[] args) throws IOException {
    File f = new File("E:\\javaProject\\coding\\test\\test.txt");
    FileInputStream fis = new FileInputStream(f);
    InputStreamReader isr = new InputStreamReader(fis, "utf-8");
    BufferedReader br = new BufferedReader(isr);

    // 开始计时
    long startTime = System.currentTimeMillis();
    char[] ch = new char[20];
    int len = br.read(ch);
    while(len != -1){
        System.out.print(new String(ch, 0, len));
        len = br.read(ch);
    }
    // 结束计时
    long endTime = System.currentTimeMillis();
    System.out.println("\n复制完成的时间为:"+(endTime-startTime));
    // 关闭流
    br.close();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值