FileWriter in java not writing to txt file [duplicate]
Asked Answered
B

3

5

I am trying to get input from a JOptionPane and store what the user typed into a text file using the FileWriter class.To make sure that the input from what the user typed was being stored I wrote a system.out and what I typed in the JOptionPane appears. Unfortunately when I open the .txt file nothing I entered appears! By the way, the file path I entered is correct.

Here is my code. HELP ME!

String playername = JOptionPane.showInputDialog("What Will Be Your Character's Name?");
System.out.println(playername);
try {
    FileWriter charectersname = new FileWriter("/Users/AlecStanton/Desktop/name.txt/");
    BufferedWriter out = new BufferedWriter(charectersname);
    out.write(playername);
    }
catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
}
Backplate answered 19/7, 2013 at 16:1 Comment(2)
Looks like you need to close your BufferedWriter.Typecase
Also remove the slash / after .txt.Camass
C
18

Buffered writers will only write out when they're full or when they're being closed (hence the name Buffered).

So you can do this:

out.close();

which will flush the buffer and then close it. If you only wanted to flush it but keep it open for further writes (e.g. imagine you're writing a log file), you could do:

out.flush();

You'd likely want to do this when finishing up with such a resource. e.g.

BufferedWriter out = ...
try {
   out.write(...);
}
catch (Exception e) {
   // ..
}
finally {
   out.close();
}

Or possibly using the try-with-resources constructs in Java 7, which (frankly) is more reliable to write code around.

Chkalov answered 19/7, 2013 at 16:6 Comment(2)
I did that, and it is not working.Cornwell
out.flush(); works for me since I have more to writeLastminute
M
4

You should close your writer in a finally block.

BufferedWriter out = null;

try {
    out = new BufferedWriter(new FileWriter("/Users/AlecStanton/Desktop/name.txt/"));
    out.write(playername);
} catch (IOException e) {
    e.printStackTrace();
} finally {
    try {
        if(out != null){
            out.close();
        } else {
            System.out.println("Buffer has not been initialized!");
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Maracanda answered 19/7, 2013 at 16:3 Comment(3)
Note that out variable scope is just in the try code block. Your answer should refactor OP's code.Typecase
A common problem here is that you need to check for out being initialised. Otherwise your finally{} block could throw an NPEChkalov
lol.. was just editing..formatting is messed up.Maracanda
C
4

The Java 7 version with the try() closing automatically.

try (BufferedWriter out = new BufferedWriter(
        new FileWriter("/Users/AlecStanton/Desktop/name.txt"))) {
    out.write(playername);
} catch (IOException e) {
    e.printStackTrace();
}

Mind the left-out / after .txt.

Camass answered 19/7, 2013 at 16:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.