栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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.2011.17copytest11”);中copytest11没有加后缀.txt
2. 代码中 out.write(in.read(chars,0,data));语句 错误,没有完成“写”的功能,应该去掉in.read修改成

 out.write(chars,0,data);		

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/532318.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号