1

I am trying to write a code for getting input from the console & the input text will be saved in a particular file. My code gets console input perfectly & create a file as well, but the console input doesn't save to the file.

Here is my code:

package com.mahbub.file_object;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Scanner;

public class ConsoleInput {

    public static void main (String [] args) throws IOException{
        BufferedReader br=null;
        BufferedWriter bw=null;
        
        File file=new File("D:/test1.txt");
        
        
        
        Reader reader=new InputStreamReader(System.in);
        br =new BufferedReader(reader);
        String str=null;
        
        do{
            System.out.println("Enter 'q' for quit!!");
            str=br.readLine();
            FileWriter fw=new FileWriter(file,true);
            bw=new BufferedWriter(fw);
            bw.write(str);
            System.out.println(str);
        }while(!(str.equalsIgnoreCase("q")));
        
    }
}

Anyone has any idea to solve this problem ?

4 Answers 4

3

You need to close or flush the Writer after writing;

try:

System.out.println("Enter 'q' for quit!!");
        str=br.readLine();
        FileWriter fw=new FileWriter(file,true);
        bw=new BufferedWriter(fw);
        bw.write(str);
        bw.flush();//You are Missing this
        System.out.println(str);

Edit: Ok I did flush as per suggestion.

But you will still need to call bw.close(); after you are done with the writer. Closing the Writer will automatically flush it.

Sign up to request clarification or add additional context in comments.

2 Comments

You should also create and open your filewriter before the loop begins, and then close it once the loop has finished
The close should be in a finally block, or you should use a try-with-resources structure
1

You need to flush() and close() the FileWriter (closing it will also flush it).

Try this :

do {
    System.out.println("Enter 'q' for quit!!");
    str=br.readLine();
    FileWriter fw=new FileWriter(file,true);
    bw=new BufferedWriter(fw);
    bw.write(str);
    bw.flush();    //do this , you missed it
    System.out.println(str);
}
while(!(str.equalsIgnoreCase("q")));

bw.close();     //do this , you missed it

Comments

0

It doesn't look like you're flushing your BufferedWriter, which may be an issue.

You also never close the BufferedWriter, which also helps since the BufferedWriter flushes itself during the close process.

Comments

0

A BufferedWriter buffers the output in a character buffer, instead of immediately sending the characters to the underlying Writer (a FileWriter in this case). This is a good practice as it reduces the number of possibly costly writes. The characters are sent to the underlying Writer when the buffer is full, or when an explicit call to flush() is made.

Secondly a Writer should always be closed, so that any used resources can be freed up again. The close() operation will automatically flush any characters remaining in the buffer before closing. (note that for Writers that do not buffer the flush() method usually does nothing)

This is basically what goes wrong in your code : since you don't flush or close the BufferedWriter, and likely write fewer characters to the file than the buffer size, your output simply remains in the buffer.

Some other things seem to be wrong in your code (that do not immediately cause the problem you report)

  • you create a new FileWriter for each string to write. Normally you'd use only one FileWriter declared before the loop. Since you create the Writer in overwrite mode, each String will simply overwrite the previous.
  • if the file test1.txt does not exist, this code will throw a FileNotFoundException when trying to create the FileWriter.
  • You use the String "q" as a marker to stop adding to the file, yet you write "q" to the file prior to checking the termination condition. I guess that's not the intent.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.