java中随机访问文件中的新行

问题描述 投票:1回答:5

每当在java中使用RandomAccessFile的'writeBytes'方法时,它都会将文本写入文件的同一行。我怎样才能使用RandomAccessFile换新线? (没有BufferedReader)。

java newline randomaccessfile
5个回答
3
投票

试试这个:

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

3
投票

你可以写一个行分隔符。为了获得当前运行的操作系统的正确行分隔符,您必须查找line.separator属性。这些方面的东西:

randomAccessFile.writeBytes(System.getProperty("line.separator"));

0
投票
System.getProperty("line.separator");

尝试在文件之前写一个新行,因为它将开始写入你上一次写入的地方。


0
投票

换行符的ASCII值为10。您可以将此值存储在字节中,然后使用“writeBytes”,这将生成一个新行。


0
投票
String data = "New Line";
RandomAccessFile raf = new RandomAccessFile(myFile);
raf.writeBytes("\r\n" + data);

这将在“text”之前添加一个新行(windows style / CRLF)。

© www.soinside.com 2019 - 2024. All rights reserved.