Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java write to the end of file with new line

Tags:

java

I want to write results to the end of the file using java

FileWriter fStream;
        try {
            fStream = new FileWriter("recallPresision.txt", true);
            fStream.append("queryID=" + queryID + "         " + "recall=" + recall + "           Pres=" + presision);
            fStream.append("\n");
            fStream.flush();
            fStream.close();
        } catch (IOException ex) {
            Logger.getLogger(query.class.getName()).log(Level.SEVERE, null, ex);
        }

I put "\n" in the statement , it writes to the file but not with new line

I want to print results with new line

like image 880
LinCR Avatar asked May 15 '12 22:05

LinCR


2 Answers

The newline sequence is system dependent. On some systems its \n, on others it's \n\r, \r\n, \r or something else entirely different. Luckily, Java has a built in property which allows you to access it:

 String newline = System.getProperty("line.separator");
like image 147
Jeffrey Avatar answered Nov 12 '22 23:11

Jeffrey


Wrong

fStream.append("\n");

Right

// don't guess the line separator!
fStream.append(System.getProperty("line.separator"));
like image 22
Andrew Thompson Avatar answered Nov 12 '22 22:11

Andrew Thompson