Search code examples
javanewlinerandomaccessfile

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).


Solution

  • 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);