Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PrintWriter create empty file

I have StringBuilder sb and I want that string save as *.txt file. Problem is that I get "filename.txt" but it is completely empty, also there is no errors in console.

Here is my code:

System.out.print(sb.toString());
PrintWriter out;
try{
    out = new PrintWriter("filename.txt");
    out.println(sb.toString());
}catch (Exception e) {
    e.printStackTrace();
}

Console output:

[19/3/2014]

Ime in priimek: Janez Novak
Naslov: Ulica 1, 1000 Ljubljana 
Telefon: 040 111 222

Registerska št.: LJ 1234
Znamka: Citroen
Model: C4
Letnik: 2005
Opombe: Popravi

Thanks!

like image 206
Matjaž Avatar asked Oct 30 '25 03:10

Matjaž


1 Answers

Either create your PrintWriter with this constructor, changing the first argument to an OutputStream:

out = new PrintWriter(new FileOutputStream("filename.txt"), true);

to turn on auto flushing, or, just close the writer once you're done writing to it with out.close().

like image 143
Martin Dinov Avatar answered Oct 31 '25 18:10

Martin Dinov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!