Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
104 views
in Technique[技术] by (71.8m points)

java - Why does the buffered writer does not write immediately when the write() method is called

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
1.Add number
2.Delete number
3.List all
4.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('
');*/
                String actual=str+","+number+"
";
                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();
    }
}
}   

in my case 2 when I try to bring back the name from the file csvv.txt it is not working because the file is actually without data. But when I run the case 1 alone the data is writtern in the file

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

BufferedWriter does not write immediately write because it buffers the data it is given.

Imagine you work in the mail room at your company. Somebody walks in with a letter to send to a client. It takes you 10 minutes to go down to the post office down the street to mail it. Do you take the letter immediately, or would you wait to see if anybody else is going to bring you a letter first?

This is buffering: instead of doing an expensive operation (walking down the street) immediately, you can wait and gather lots of things to do at once - if you have 100 letters to mail, it will still take you just 10 minutes to walk down the street, even if it takes you marginally longer to stuff them in the mail box.

It's the same with IO on a computer: it's expensive. Writing to disk, sending to the network etc are slow, so you don't want to do them repeatedly if you don't have to.

But should you care that buffering is taking place? Largely, no. In the mail room example, people just want to drop off their letters, and know it will be delivered at some point in the future. And once they've dropped it off, it doesn't matter to them whether you run down the street now or wait for 100 letters first.

In code, you often don't care when the data gets written. It just gets written at some point, e.g. when the file writer is closed, or once you've asked to write a certain amount of data to the file.

If you care about the data being written before one of these things happen, you can call bw.flush() to force it to happen immediately.

You can read more about IO and buffering in Oracle's Essential Java tutorial. You can start here, but the bit about buffering is here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...