user2573658
user2573658

Reputation: 11

Trying to bufferedWrite to a txt file and it will not work

I am having trouble making the bw.write(line) call work. Here is my code:

    InputStreamReader fr = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(fr);

    FileWriter fw = new FileWriter("Yes.txt");
    BufferedWriter bw = new BufferedWriter(fw);

    String line;
    while ((line = br.readLine()) != null ) {
        System.out.print(line);
        bw.write(line); //line causing the issue
    }

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

Does anyone know what I am doing wrong?

Upvotes: 1

Views: 193

Answers (1)

chancea
chancea

Reputation: 5968

Most likely you need to call .flush() after you are done writing. BufferedWriter is exactly what it sounds like: It writes to a buffer first and once that buffer is full it outputs what is contained within the buffer. Calling flush() will flush out what is contained within the buffer.

Upvotes: 1

Related Questions