[英]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. 每当在Java中使用RandomAccessFile
的'writeBytes'方法时,它都会在文件的同一行中写入文本。 How can I get to a new line with RandomAccessFile
only? 如何仅使用RandomAccessFile
转到新行? (No BufferedReader
). (没有BufferedReader
)。
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. 为了获得当前正在运行的操作系统的正确的行分隔符,您必须寻找line.separator
属性。 Something along these lines: 遵循以下原则:
randomAccessFile.writeBytes(System.getProperty("line.separator"));
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);
System.getProperty("line.separator");
尝试在文件之前写入新行,因为它将开始写入上次停止写入的位置。
new line character has an ASCII value of 10 . 新行字符的ASCII值为10。 You can store this value in in a Byte and then use the "writeBytes" which will result in a new line. 您可以将此值存储在一个字节中,然后使用“ writeBytes”,这将导致换行。
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)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.