how to write to a file with \n (new line)?

Hi there,

I have the following method that reads a file's content and convert it into a string:

publicstatic String getFileContent(String fileName)throws IOException

{

File file =new File(fileName);

FileInputStream fileInputStream =new FileInputStream(file);

BufferedReader bufferedReader =new BufferedReader(new InputStreamReader(fileInputStream));

StringBuffer stringBuffer =new StringBuffer();

String line;

while ( (line=bufferedReader.readLine()) !=null )

{

stringBuffer.append(line+"\n");

}

return stringBuffer.toString();

}

I'm trying to write a method that writes a String to a file WITH THE NEW LINE. All classes I used (DataOutputStream, FileWriter, PrintStream...) do not force a new line (I get everything in one line)

I thought maybe I should change the "\n" in the line

stringBuffer.append(line+"\n");

but...didn't know what.

thanks for any advise

[1515 byte] By [xianwinwina] at [2007-11-26 20:21:53]
# 1
I for one don't understand your question.Your code doesn't do any file writing at all.If you have a Writer and you want to write a new line to it, then you can just do writer.write("\n"). It's probably easier to wrap it in a PrintWriter though.
paulcwa at 2007-7-10 0:47:03 > top of Java-index,Java Essentials,Java Programming...
# 2
use PrintWriter's println method.
PMJaina at 2007-7-10 0:47:03 > top of Java-index,Java Essentials,Java Programming...
# 3

BufferedReader readLine remove the newline sequence. But you're putting \n back which should do what you appear to want.

However newline sequence isn't always just \n, on unix systems it's \n, on windows its \r\n (reads "carriage-return, linefeed"). Sytem.getProperty("line.separator"); will get you the appropriate end of line string.

malcolmmca at 2007-7-10 0:47:03 > top of Java-index,Java Essentials,Java Programming...