aceBox aceBox - 6 months ago 16
Java Question

new line in Random Access File in java

Whenever using the 'writeBytes' method of

RandomAccessFile
in java,it writes the text in the same line in the file. How can I get to a new line with
RandomAccessFile
only? (No
BufferedReader
).

Answer

Try this:

 RandomAccessFile file = new RandomAccessFile("e:\\demo.txt","rw");
 String originalString = "First line \nSeconf line \n";
 String updatedString = originalString.replace("\n","\r\n");
 file.writeBytes(updatedString);
Comments