Why does the buffer writer not write immediately after calling the write () method

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();
    }
}
}   

in my case 2, when I try to return the name from the csvv.txt file, it does not work because the file does not actually contain data. But when I run only one case, the data is written to the file

+3
source share
1 answer

BufferedWriter It doesn’t record immediately, because it buffers the data it sets.

Imagine that you work in a mail room in your company. Someone walks with a letter to send to a client. You need 10 minutes to go down to the post office on the street to send it by mail. Are you taking the letter immediately or will you wait for someone else to send you the letter?

: , ( ), , - 100 , 10 , , .

IO : . , .. , , .

, ? . , - . , , 100 .

, . - , . , .

, , bw.flush(), .

IO Oracle Essential Java. ,

+7

Source: https://habr.com/ru/post/1615733/


All Articles