Java txt text append, line-wrapping problem __java

Source: Internet
Author: User

Public void Test () throws exception{

File File = new file ("C:/test.log");

Randomaccessfile RF = new randomaccessfile (file, "RW");

long filelength = Rf.length ();

Move this pointer to the end of the text

Rf.seek (filelength);

Text file append text, line feed

Rf.writebytes ("11111/n");

Rf.writebytes ("1111/r/n")

Rf.close ();

}

Rf.writebytes ("11111/n");

The program executes the above statement:

There are a lot of small black blocks in the text file, because there is only one character in the file,

That is, 10 of the ASCII code, and in Windows, the newline is represented by two characters, that is, the carriage return plus line, which is 13 and 10 of the ASCII code.

When you open such a file,

Because in this file only newline, no return, so the Notepad program to parse it into a small black block.

Solution:

System.out.println (System.getproperty ("Line.separator"). Length ());//2

String str = system.getproperty ("Line.separator");

for (int i=0;i<str.length (); i++)

Print ASCII Code

System.out.print ((int) Str.charat (i) + ""); 13 10

System.out.println ();

---------------------------------------------

long filelength = Rf.length ();

String lineseparator = System.getproperty ("Line.separator");
Rf.writebytes ("11111" +lineseparator);

First execution filelength = 0

Second execution Filelength = = 7//11111 + "/r/n"

---------------------------------------------------------------------

Third execution filelength = 14

System.getproperty in Java ("Line.separator"). Length () = 2

Writing characters to a text file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.