java IO流之FileReader FileWriter 处理文件 复制文件内容

75 篇文章 2 订阅

java IO流之FileReader FileWriter处理文件 复制文件内容

在这里插入图片描述

分步实现

  1. 创建读写文件对象
//1.1有一个源文件
File f1 = new File("IOStream/src/testfile/test.txt");
//1.2有一个目标文件:
File f2 = new File("IOStream/src/testfile/demo.txt");
  1. 接“管”,创建FileReader,FileWriter流

    //2.1搞一个输入的管 怼到源文件上:
    FileReader fr = new FileReader(f1);
    //2.2搞一个输出的管,怼到目标文件上:
    FileWriter fw = new FileWriter(f2);
    
  2. 文件内容操作

    1. 一个字符一个字符的复制

      int n = fr.read();
      while(n!=-1){
      	fw.write(n);//此处可修改成想要的输出
      	n = fr.read();
      }
      
    2. 利用缓冲字符数组

      char[] ch = new char[5];
      int len = fr.read(ch);
      while(len!=-1){
      	fw.write(ch,0,len);//将缓冲数组中有效长度写出
      	len = fr.read(ch);
      }
      
    3. 将缓冲字符数组转为String

      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);
      }
      
  3. 关闭流:(关闭流的时候,倒着关闭,后用先关)

    fw.close();
    fr.close();
    

完整程序

搭配异常处理后的代码

public class RW {
    public static void main(String[] args){
        //1.1有一个源文件
        File f1 = new File("IOStream/src/testfile/test.txt");
        //1.2有一个目标文件:
        File f2 = new File("IOStream/src/testfile/demo.txt");
        //2.1搞输入的管和输出的管
        FileReader fr = null;
        FileWriter fw = null;
        try {
            //2.2.1将输入的管怼到源文件上
            fr = new FileReader(f1);
            //2.2.2将输出的管怼到目标文件上
            fw = new FileWriter(f2);
            //3利用缓冲字符数组,将数组转为String写出
            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 (IOException e) {
            e.printStackTrace();
        } finally {
            //4.关闭流:(关闭流的时候,倒着关闭,后用先关)
            try {
                assert fw != null;//防止空指针异常
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                assert fr != null;
                fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

也可已使用try-with-resource自动关闭资源

public class RWTC {
    public static void main(String[] args){
        //1.1有一个源文件
        File f1 = new File("IOStream/src/testfile/test.txt");
        //1.2有一个目标文件:
        File f2 = new File("IOStream/src/testfile/demo.txt");
        //2.1搞输入的管和输出的管,并分别接到源文件和目标文件上
        try (
                FileReader fr = new FileReader(f1);
                FileWriter fw = new FileWriter(f2);
                ){
            //3利用缓冲字符数组,将数组转为String写出
            char[] ch = new char[5];
            int len = fr.read(ch);
            while(len!=-1){
                String s = new String(ch,0,len);
                fw.write(s + "\n");
                len = fr.read(ch);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.out.println("找不到文件");
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SK Primin

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值