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 ?