每当在java中使用RandomAccessFile
的'writeBytes'方法时,它都会将文本写入文件的同一行。我怎样才能使用RandomAccessFile
换新线? (没有BufferedReader
)。
试试这个:
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);
你可以写一个行分隔符。为了获得当前运行的操作系统的正确行分隔符,您必须查找line.separator
属性。这些方面的东西:
randomAccessFile.writeBytes(System.getProperty("line.separator"));
System.getProperty("line.separator");
尝试在文件之前写一个新行,因为它将开始写入你上一次写入的地方。
换行符的ASCII值为10。您可以将此值存储在字节中,然后使用“writeBytes”,这将生成一个新行。
String data = "New Line";
RandomAccessFile raf = new RandomAccessFile(myFile);
raf.writeBytes("\r\n" + data);
这将在“text”之前添加一个新行(windows style / CRLF)。