2021.11.17 Java FileReader、FileWriter、使用两者拷贝普通文本文件

FileReader
文件字符输入流,只能读取普通文本。
读取文本内容时,比较方便,快捷。

    public static void main(String[] args) {
        FileReader fir = null;
        try {
            fir = new FileReader("myfile");
 //一个一个读出来
            char[] c = new char[8];
            for (char s: c) {
                System.out.println(s);
            }
//或者用另一种方式:
            char[] c1 = new char[8];
            int count=0;
            while ((count=fir.read())!=-1){
                System.out.println(new String(c1,0,count));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {

        } finally {
            try {
                fir.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
  }

错误信息:
Exception in thread “main” java.lang.StringIndexOutOfBoundsException: String index out of range: 97
at java.lang.String.(String.java:205)
at com.exception.test.test.main(test.java:18)
错误原因:第18行中count=fir.read() 其中没有传入char c1;
应该改成:

while ((count=fir.read(c1))!=-1)

已解决------------------------------------------------

控制台显示异常
在这里插入图片描述
异常原因:
在这里插入图片描述
其中定义了char c 但是没有往数组中读;
补充语句:fir.read(c);
在这里插入图片描述

FileWriter
文件字符输出流,写。只能输出普通文本。

public static void main(String[] args) {
    FileWriter out =null;
    try {
        out = new FileWriter("myfile",true);
        out.write("\n");
        char[] c = {'我','是','我','的'};
        out.write(c);
        out.write(c,1,2);
        out.write("aasdhiaiwhhwidawda");
        out.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }finally {
        try {
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

拷贝普通文本文件
此处普通文件定义:能用记事本打开的都是普通文件(不仅限于.txt)。

public static void main(String[] args) {
        FileReader in =null;
        FileWriter out=null;
        try {
            in = new FileReader("E:\\得邦照明公司\\test.txt");
            out = new FileWriter("E:\\得邦照明公司\\2021.11.15-11.20\\11.17\\copytest11");
            int data =0;
            char[] chars = new char[1024*512];
            while((data = in.read(chars))!=-1){
                out.write(in.read(chars,0,data));
            }
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

控制台没报错,但是复制文件没内容并且文件格式异常。FileWriter拷贝文本文件没有内容。
在这里插入图片描述
异常原因:
1.out = new FileWriter(“E:\得邦照明公司\2021.11.15-11.20\11.17\copytest11”);中copytest11没有加后缀.txt
2. 代码中 out.write(in.read(chars,0,data));语句 错误,没有完成“写”的功能,应该去掉in.read修改成

 out.write(chars,0,data);		

·····················添加修改后成功解决

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值