BufferedWriter如何在Java中工作 [英] How does BufferedWriter work in java

查看:124
本文介绍了BufferedWriter如何在Java中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常将文本输出到文件。我想知道一个问题: BufferedWriter 如何工作?

I frequently output text to files. I wonder something: how does BufferedWriterwork?

当我调用<$ c $时,它是否在文件上写文本? c> writer.write(text)?
如果不写文本,是否需要使用冲洗功能来写数据?

Does it write text on file when I call writer.write(text)? If it doesn't write text,do I need to use flush function to write data?

例如:

       File file = new File("statistics.txt");
        if (!file.exists()) {
            file.createNewFile();
        }
        else
        {
            file.delete();
            file.createNewFile();
        }
        FileWriter fileWritter = new FileWriter(file.getAbsoluteFile(),true);
        BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
        Iterator<Map.Entry<String,Token>> it = listofTakenPairMap.entrySet().iterator();
        int isim_isim=0;
        int isim_fiil=0;
        int zarf_fiil=0;
        while (it.hasNext()) {
            @SuppressWarnings("rawtypes")
            Map.Entry pairs = (Map.Entry)it.next();
            Token token=(Token)pairs.getValue();
            String str=pairs.getKey()+ " = " +token.getCount();
            if(token.getTypeOfPairofToken()==0){//isim-isim
                isim_isim+=token.getCount();
            }
            else if(token.getTypeOfPairofToken()==1){
                isim_fiil+=token.getCount();
            }
            else{ //zarf-fiil
                zarf_fiil+=token.getCount();
            }
            System.out.println(str);
            bufferWritter.write(str);
            bufferWritter.newLine();
            //it.remove(); // avoids a ConcurrentModificationException
        }
        bufferWritter.newLine();
        bufferWritter.write("##############################");
        bufferWritter.newLine();
        bufferWritter.write("$isim_isim sayisi :"+isim_isim+"$");
        bufferWritter.newLine();
        bufferWritter.write("$isim_fiil sayisi :"+isim_fiil+"$");
        bufferWritter.newLine();
        bufferWritter.write("$zarf_fiil sayisi :"+zarf_fiil+"$");
        bufferWritter.newLine();
        bufferWritter.write("##############################");
        bufferWritter.flush();
        bufferWritter.close();

如果while循环中发生错误,该文件将被关闭而不写入数据。如果在while循环中使用 flush 函数,那为什么要使用 BufferedWriter ?如果我错了,请纠正我。

If an error occurs in the while loop,the file will be closed without writing data. If I use flush function in the while loop,then why should i use BufferedWriter? Please correct me If I'm wrong.

推荐答案

根据定义,缓冲的写入器将缓冲数据并仅在有足够的数据时才写入数据

By definition, a buffered writer buffers data and only writes them when it has enough in memory, to avoid too many roundtrips to the file system.

如果可以正确处理异常,并像往常一样在finally块中关闭流,则可以使用缓冲区将被刷新到磁盘,并且到目前为止写入缓冲写入器的所有内容都将被写入磁盘(当然,例外情况恰好是由于写入磁盘错误引起的。)

If you handle exceptions properly, and close your stream in a finally block as you should always do, the buffer will be flushed to the disk, and everything written to the buffered writer so far will be written to the disk (unless of course the exception is precisely caused by an error writing to the disk).

因此,解决方案是不要在每次写入ou时都刷新,因为这样做会破坏缓冲写入器的目的。解决方案是在finally块中关闭(或使用Java 7 trye-with-resources语句为您完成此操作)。

So, the solution is not to flush each time ou write, since it would defeat the purpose of the buffered writer. The solution is to close in a finally block (or to use the Java 7 trye-with-resources statement, which does that for you).

这篇关于BufferedWriter如何在Java中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆