Java input/output (I/O) Learning notes-file input and output

Source: Internet
Author: User
Tags java se

When you save your data, you can choose either binary or text format. For example, when the integer 1234 is stored as a binary number, it is written as a sequence of bytes D2 (hexadecimal notation), and stored in text format, it is saved as the string "1234". Although I/O in binary format is fast and efficient, it is not suitable for people to read.

Read and write text data

When storing text strings, you need to consider the character encoding (character encoding) method. In UTF-16 encoding, the string "1234" is encoded as 00 31 00 32 00 33 00 34 (hexadecimal). However, many programs want text files to be encoded in other ways. The OutputStreamWriter class converts a stream of Unicode characters into a byte stream using the selected character encoding. Instead, the InputStreamReader class converts an input stream containing bytes (characters that are represented by a character encoding) into a reader that can produce a Unicode code element.
You can choose a different encoding method by specifying it in the InputStreamReader constructor. For example:

innew InputStreamReader(new FileInputStream("test.dat""ISO8859_5");

For text output, you can use PrintWriter. This class has a method of printing strings and numbers in text format, and it even has a convenient way to link PrintWriter to FileWriter, the following statement:

outnew PrintWriter("test.txt");

Equivalent to:

outnew PrintWriter(new FileWriter("test.txt"));

In order to output to the print writer, you need to use the same print, println, and printf methods as when using System.out.

"Harry Hacker";45000;out.println" " + salary);out.close();

It will put the following characters:

Harry Hacker 45000.0

Output to the writer out, after which the characters will be converted into bytes and eventually written to Test.txt.
For text input, the only way to process text input before Java SE 5.0 is through the BufferedReader class, which has a readLine method that allows us to read a line of text. You need to combine the reader with the buffer with the input source:

innew BufferedReader(    new InputStreamReader(    new FileInputStream("test.txt""UTF-8"));

However, BufferedReader does not have any methods for reading numbers, so it is recommended that you use Scanner to read into text input.

Read and write binary data

The DataOutput interface defines a complete set of methods for writing arrays, characters, Boolean values, and strings in binary format. In order to read back the data, you can use the relative methods defined in the Datainput interface.
The DataInputStream class implements the Datainput interface, in order to read binary data from a file, you can combine datainputstream with a byte source, such as FileInputStream:

innew DataInputStream(new FileInputStream("test.dat"));

Similarly, to write binary data, you can use the DataOutputStream class that implements the DataOutput interface:

outnew DataOutputStream(new FileOutputStream("test.dat"));

The Randomaccessfile class can find or write data anywhere in the file. Disk files are randomly accessed, but data flows from the network are not. You can open a random access file for reading only or for both read and write, and we can specify this option by using the string "R" (for read-in Access) or "RW" (for read/write access) as the second parameter of the constructor.

innew RandomAccessFile("test.dat""r"inOutnew RandomAccessFile("test.dat""rw");

When you open an existing file as a randomaccessfile, the file is not deleted.
A random Access file has a file pointer that represents the next byte to be read in or written out, and the Seek method can set the file pointer to any byte position in the file, and the Seek parameter is a long integer whose value is between 0 and the length of the file measured in bytes.
The Getfilepointer method returns the current position of the file pointer.
The Randomaccessfile class implements both Datainput and DataOutput interfaces, which can be randomly read and written to a file.

To write data in text format, you need to use PrintWriter.
To read data in text format, you need to use scanner.
To write data in binary format, you need to use DataOutputStream.
To read data in binary format, you need to use DataInputStream.

Java input/output (I/O) Learning notes-file input and output

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.