为什么缓冲的writer在调用write()方法时不会立即写入

时间:2015-11-13 10:41:46

标签: java

package javaapplication11;

import java.util.Scanner;
import java.io.*;


/**
 *
 * @author jenison-3631
 */
public class JavaApplication11 {

    /**
     * @param args the command line arguments
     * @throws java.io.IOException
     */

    public static void main(String[] args) throws IOException
    {

       // TODO code application logic here
       // File file = new File("/Users/jenison-3631/Desktop/csvv.txt");
        int n,z=1;
        FileWriter writr = new FileWriter("/Users/jenison-3631/Desktop/csvv.txt");
        FileReader fr= new FileReader("/Users/jenison-3631/Desktop/csvv.txt");
        BufferedReader br=new BufferedReader(fr);
        BufferedWriter bw= new BufferedWriter(writr);
        try{
        while(z==1)
        {
            System.out.println("please enter your choice\n1.Add number\n2.Delete number\n3.List all\n4.Search number");
            Scanner s= new Scanner(System.in);
            n= s.nextInt();
            switch(n)
            {
                case 1:

                String str;
                String number;
                System.out.println("Enter the name");
                s.nextLine();                       
                str= s.nextLine();
                System.out.println("Enter the number");
                number=s.nextLine();   
                System.out.println(str+" "+number);

               /* writer.append(str);
                writer.append(',');
                writer.append(number);
                writer.append('\n');*/
                String actual=str+","+number+"\n";
                bw.write(actual,0,actual.length());
                break;

                case 2:
                String del=null;
                String line=null;
                String spl=",";
                System.out.println("Enter the name whose phone number should be deleted");
                s.nextLine();
                del=s.nextLine();

                while((line=br.readLine())!=null)
                {
                    String[] country = line.split(spl);
                    System.out.println("hai"+country[0]);
                }


                System.out.println(del);
                break;


            }
            System.out.println("Do u wish to continue....if yes press 1 else press 2");           

            z= s.nextInt();
        }
    }
    finally{
    bw.close();
    br.close();
    }
}
}   

在我的情况2中,当我尝试从文件csvv.txt中恢复名称时,它无法正常工作,因为该文件实际上没有数据。但是,当我单独运行案例1时,数据在文件

中写入

1 个答案:

答案 0 :(得分:9)

BufferedWriter不会立即写入,因为它缓冲它给出的数据。

想象一下,你在贵公司的邮件室工作。有人带着信寄到客户端。你需要10分钟才能到街上的邮局寄邮件。你是否立刻接受了这封信,还是等着看其他人是否会先给你写信?

这是缓冲:而不是立即进行昂贵的操作(在街上行走),你可以等待并立即收集很多事情 - 如果你有100封邮件信件,它仍然只需要10分钟走在街上,即使你把它们放在邮箱里也要花费更长的时间。

计算机上的IO也是如此:它很昂贵。写入磁盘,发送到网络等是很慢的,所以如果你不需要,你不想重复这些。

但你是否应该关心缓冲是否正在发生?很大,没有。在邮件室的例子中,人们只想放下他们的信件,并知道它将在未来的某个时刻送达。一旦他们放弃它,无论你现在在街上奔跑还是先等待100个字母都无关紧要。

在代码中,您通常不关心何时写入数据。它只是在某些时候写的,例如当文件编写器关闭时,或者一旦你要求将一定数量的数据写入文件。

如果您在其中一件事发生之前关心正在撰写的数据,您可以致电bw.flush()以强制它立即发生。

您可以在Oracle的Essential Java教程中阅读有关IO和缓冲的更多信息。您可以start here,但the bit about buffering is here

相关问题