java java中随机访问文件中的新行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14611843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
new line in Random Access File in java
提问by aceBox
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
).
每当使用RandomAccessFile
java 中的 'writeBytes' 方法时,它都会将文本写入文件的同一行中。我怎样才能到达一个新的线路RandomAccessFile
?(否BufferedReader
)。
回答by Marc Baumbach
You can write a line separator. In order to get the correct line separator for the currently running operating system, you'll have to look for the line.separator
property. Something along these lines:
您可以编写行分隔符。为了为当前运行的操作系统获得正确的行分隔符,您必须查找该line.separator
属性。沿着这些路线的东西:
randomAccessFile.writeBytes(System.getProperty("line.separator"));
回答by Hemant Kumar
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);
回答by Vihaan Verma
new line character has an ASCII value of 10 . You can store this value in in a Byte and then use the "writeBytes" which will result in a new line.
换行符的 ASCII 值为 10 。您可以将此值存储在一个字节中,然后使用“writeBytes”,这将产生一个新行。
回答by Maker
String data = "New Line";
RandomAccessFile raf = new RandomAccessFile(myFile);
raf.writeBytes("\r\n" + data);
This will add a new line (windows style / CRLF) before the "text".
这将在“文本”之前添加一个新行(Windows 样式/CRLF)。
回答by Matt Clark
System.getProperty("line.separator");
Try writing a new line to the file before anything else, as it will start writing where your last write left off.
尝试在其他任何事情之前向文件写入一个新行,因为它会从上次写入停止的地方开始写入。