Creating a file and writing to it
<p>I created a file named 'test.txt' and then took input from the user to write the input to the file. Everything runs fine. The program doesn't show any error at all. The file is created and the program takes input from the user but when I checked the content of the file, it was empty. Can anyone figure out what is wrong with my code? The code is as follows. </p>

<pre><code>package InputOutput;

import java.io.*;

public class CharacterFileReaderAndFileWriter{

private BufferedReader br = null;
private BufferedWriter bw = null;
private PrintWriter pw = new PrintWriter(System.out, true);

public File createFile() throws IOException{
    File f = new File("test.txt");
    return f;
}

public void writeToFile() throws IOException{
    try{
        bw = new BufferedWriter(new FileWriter(createFile()));
    }
    catch(FileNotFoundException ex){
        ex.printStackTrace();
    }

    //take input from the console (user)
    br = new BufferedReader(new InputStreamReader(System.in));
    String s;

    pw.println("Please enter something");
    pw.println("To stop the program, enter 'stop'");

    do{
        s = br.readLine();
        if(s.compareTo("stop")==0)
            break;
        s+= "\r\n";//adding an new line to the string s         
        bw.write(s);            
    }
    while(s.compareTo("stop")!=0);

    br.close();
    bw.close();

}

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

    CharacterFileReaderAndFileWriter cfr = new CharacterFileReaderAndFileWriter();
    cfr.writeToFile();
}
</code></pre>

<p>}</p>
