JAVA file operations

Source: Internet
Author: User

The usage of random streams is similar to that of other languages (C/PHP). Therefore, it is used in the end and has powerful functions.

The code is as follows: Copy code

RandomAccessFile objectOut = new RandomAccessFile (accountNumber + "_info.txt", "rw ");

A bunch of read and write operations are provided. By default, they are from the beginning. If you want to change the position, use seek (length). You can use the built-in length () method to obtain the length, in this way, the final position can be located.

The effects of readLine () and readUTF () are similar, but different. readLine () is not supported for Chinese. If you need readLine to read Chinese, you may need to output this output.

The code is as follows: Copy code

String str = objectOut. readLine ();
Byte B [] = str. getBytes ("ISO-8859-1 ");

However, readUTF () can be used directly, and writeUTF () can be used for writing.

In addition, there is no need to break the line too much. Each read operation corresponds to each write operation, and the current string is located at the end of each read operation. Therefore, you only need to write the statement as follows:

The code is as follows: Copy code

ObjectOut. writeUTF (String. valueOf (accountNumber ));
ObjectOut. writeUTF (accountName );
ObjectOut. writeDouble (balance );
ObjectOut. writeUTF ("current account ");

You can.

You can use getFilePointer () to obtain the current location.

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.