| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1782 人关注过本帖
标题:bufferdeoutputwriter
只看楼主 加入收藏
郭赛
Rank: 1
等 级:新手上路
帖 子:31
专家分:0
注 册:2017-12-9
结帖率:77.78%
收藏
已结贴  问题点数:20 回复次数:3 
bufferdeoutputwriter
程序代码:
public class BufferedReader_demo01 {

    public static void main(String[] args) {
        File file = new File("C:\\Users\\郭赛\\Desktop\\demo03.txt");
        FileInputStream fis = null;
        InputStreamReader inputStreamReader = null;
        BufferedReader bufferedReader = null;
        FileOutputStream fos = null;
        OutputStreamWriter osr = null;


        try {
            /**
             * 建立字节流和字符流的关系
             */
            fis = new FileInputStream(file);
            inputStreamReader = new InputStreamReader(fis);
            fos = new FileOutputStream(new File("C:\\Users\\郭赛\\Desktop\\demo04.txt"));
            osr = new OutputStreamWriter(fos);
            char[] bytes = new char[1024];
            int a;
            while((a = inputStreamReader.read(bytes)) != -1)
                osr.write(bytes,0,a);

            bufferedReader = new BufferedReader(inputStreamReader);
            String encoding = inputStreamReader.getEncoding();
            System.out.println(encoding);
            String s;
            while ((s =  bufferedReader.readLine()) != null)
                System.out.println(s);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(bufferedReader == null)
                bufferedReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(inputStreamReader == null)
                    inputStreamReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(fis == null)
                    fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(osr == null)
                    osr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(fos == null)
                    fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }


        }

    }
}

为什么每次使用这个方法 while((a = inputStreamReader.read(bytes)) != -1)
                osr.write(bytes,0,a);
文件中的内容就会消失
搜索更多相关主题的帖子: null catch new try File 
2020-06-02 17:31
郭赛
Rank: 1
等 级:新手上路
帖 子:31
专家分:0
注 册:2017-12-9
收藏
得分:0 
我加上flush或者close就会输出结果,这是为什么
2020-06-02 17:58
双子座私语
Rank: 3Rank: 3
等 级:论坛游侠
威 望:3
帖 子:19
专家分:141
注 册:2020-5-6
收藏
得分:14 
你这个出问题的while是什么逻辑,write方法是输出流的输出方法,这里是把数据写到文件里面,但是你为什么加个while判断?判断的条件还是输入流读写完毕?
2020-06-03 14:53
郭赛
Rank: 1
等 级:新手上路
帖 子:31
专家分:0
注 册:2017-12-9
收藏
得分:0 
回复 3楼 双子座私语
读和写是同步进行的啊,每次从流中读出一个就写一个字符到文件中
2020-06-04 22:06
快速回复:bufferdeoutputwriter
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015433 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved