Java basic notes (3: files and data streams), java data streams

Source: Internet
Author: User

Java basic notes (3: files and data streams), java data streams
I,Input stream and output stream

The input stream loads data from files, standard inputs, or other external input devices to the memory. The function of the output stream is the opposite. Data in the memory is saved to a file or transmitted to the output device. The input stream corresponds to the abstract class Java. io. InputStream and its subclass in the java language. The output stream corresponds to the abstract class java. io. OutputStream and its subclass. Abstract classes java. io. InputStream and java. io. OutputStream define basic operations for input and output streams.

(1) InputStream and FileInputStream

Because java. io. InputStream is an abstract class, you cannot use "new InputStream ()" to construct java. io. InputStream instance objects. However, it defines the basic operations of the input stream, such as reading data and closing the input stream. System. in is a variable of the java. io. InputStream type. It corresponds to standard input and is mainly used to accept input from the keyboard.

Class java. io. FileInputStream is a subclass of the abstract class java. io. InputStream. Create an instance object like java. io. FileInputStream. You can create an instance object through the java. io. FileInputStream constructor. The basic steps for file content operations are as follows:

1) Create the input/output stream or reader instance object corresponding to the file to obtain related system resources, for example, memory space for storing the file information and the permission to control the file;

2) read (input)/write (output) the file;

3) Finally, call the close member method to close the file to release the system resources occupied.

(2) OutputStream and FileOutputStream

The abstract class java. io. OutputStream is used to process the output stream. It defines various basic operations of the output stream, such as writing and closing the output stream. System. out is a variable of the java. io. OutputStream type. It corresponds to standard output and is mainly used to output information in the console window. Generally, after calling the write member method of the abstract class java. io. OutputStream, the forced output member method of the abstract class java. io. OutputStream is often called to flush (). This is often necessary because the current computer system often uses a caching mechanism to improve operational efficiency. In this way, after calling the member method write, data is often not directly output or written to the file, but is temporarily stored in the cache. When the data is accumulated to a certain extent, the data is actually output. Call the member method flush to force immediate data output. In this way, after calling the member method flush, the output result is usually immediately displayed.

Class java. io. FileOutputStream is a subclass of the abstract class java. io. OutputStream. Create a class java. io. fileOutputStream's instance object is generally completed through its constructor. This instance object can call the corresponding write member method to write data to a file and close the output stream by closing the member method. The procedure for operating the file is as follows:

1) Create an instance object like java. io. FileOutputStream to obtain related file resources;

2) write data to the file through the write member method of java. io. FileOutputStream; in the middle, force the output through the flush member method of java. io. FileOutputStream;

3) Finally, call the close member method of java. io. FileOutputStream to close the file to release the system resources occupied.

Exceptions may occur when java. io. FileOutputStream-like instance objects are created and when java. io. FileOutputStream-like write member methods are called. Therefore, the try-catch structure or try-catch-finally structure is generally used to handle the exception.

(3) PrintStream

Class java. io. PrintStream is a very important output stream class. System. out is a variable of the java. io. PrintStream type. Java. io. PrintStream has excellent features:

1) it contains different member methods that can be used to directly output multiple types of data;

2) most of its member methods do not throw an exception;

3) You can choose whether to use the automatic force output (flush) feature. If the automatic forced output feature is used, when the output returns to another row, the data in the cache is generally automatically written to the specified file or displayed in the console window.

(4) Data input stream and output stream

The corresponding classes of the data input stream and output stream are respectively java classes. io. dataInputStream and java. io. dataOutputStream is mainly used to read and store data of the basic data type, and each basic data type stores the same number of bytes as it occupies in the memory, for example: INTEGER (int) the data type occupies 4 bytes. Because the data stream storage format adopts a uniform form, the platform of the data input stream and the output stream is less relevant. Generally, the input stream and the output stream should be used together. For example, you can use the data input stream (DataInputStream) to read data stored in the data output stream (DataOutputStream.

(5) Input stream and output stream with Cache

The corresponding classes of the cached input stream and output stream are java. io. BufferedInputStream and java. io. BufferedOutputStream. These two classes further enhance the efficiency of reading and storing data from the Input Stream and Output Stream through the cache mechanism. When java. io. bufferedInputStream or java. io. when the Instance Object of BufferedOutputStream is used, a storage unit (usually called cache) of a byte array is opened in the memory to store data in the end of the data stream. In this way, a large data block can be read into the memory, or a large data block in the memory can be written to the specified file at one time by means of byte array caching, to improve the read/write efficiency.

(6) Redirection of standard input/output streams

Java. lang. System contains three static member domains: in, out, and err, which respectively represent the standard input stream, standard output stream, and standard error output stream. The standard input stream is mainly used to accept keyboard input. The standard output stream is used to output information in the console window. The standard error output stream is used to output error message in the console window. Because they are static member domains of the java. lang. System class, they can all be accessed directly by class names, namely System. in, System. out, And System. err. They are of the following types: java. io. InputStream, java. io. PrintStream, and java. io. PrintStream. Therefore, you can call the member methods of these classes to perform various operations.

The redirection of standard input stream, standard output stream, and standard error output stream is to establish a correspondence relationship between these standard input stream, standard output stream, and standard error output stream and the specified file respectively; when you need to input data, the data will be read from the file; when you need to output data, the data will be written to the file.

 

II, Reader

The input stream and output stream both access files based on bytes and can be considered to process byte streams. The reader accesses a file based on characters, so it can be considered that the reader processes the ghost stream.

(1) Reader and Writer

Abstract classes java. io. Reader and abstract classes java. io. Writer define some basic operations on the Reader. The abstract class java. io. Reader is used to read data, and the abstract class java. io. Writer is used to store data.

You cannot directly use "new Reader ()" or "new Writer ()" to generate an abstract class java. io. reader or abstract class java. io. writer instance objects, which can only be generated by subclasses of abstract classes.

(2) FileReader and FileWriter

Classes java. io. FileReader and java. io. FileWriter are subclasses of the abstract classes java. io. Reader and the abstract classes java. io. Writer. Java. io. FileReader is compatible with all member methods of the abstract class java. io. Reader. It can be used to read characters or close the character stream. Java. io. FileWriter is compatible with all member methods of the abstract class java. io. Writer. It can output a single or multiple characters, force output, and disable the producer stream.

(3) Cache Reader

Java. io. BufferedReader, java. io. LineNumberReader, and java. io. BufferedWriter are reader classes with cache. These cache classes further enhance the reader's efficiency in reading and storing data through the cache mechanism. Class java. io. BufferedReader and class java. io. LineNumberReader inherit all member methods of the abstract class java. io. Reader and can read characters or close the upstreaming stream. The class java. io. BufferedWriter inherits all member methods of the abstract class java. io. Writer and can output one or more characters, force output, and disable the producer stream.

(4) PrintWriter

Class java. io. PrintWriter is a very important pipeline stream class. It is very similar to java. io. PrintStream. The class java. io. PrintStream is more powerful in processing bytes, while the class java. io. PrintWriter is superior in character processing. Similar to java. io. PrintStream, the java. io. PrintWriter class has excellent features:

1) Most Member methods of java. io. PrintWriter do not throw an exception. If you need to check whether an error occurs, you can use the PrintWriter member method public boolean checkError (). If an error occurs, the member method returns true; otherwise, false.

2) java. io. PrintWriter class implements all print member methods of java. io. PrintStream class. Therefore, PrintWriter also has different member methods that can directly output multiple types of data.

3) the automatic force output (flush) function of java. io. PrintWriter is different from that of java. io. PrintStream. When PrintWriter is used, only the println, printf, or format member methods can automatically force the output.

(5) Read data from the console window

Class java. io. inputStreamReader and java. io. outputStreamWriter is a common function class for reading data from the console. These two classes can be used to convert the input stream and output stream into the corresponding reader to facilitate text data processing.

 

III, File

When the File that needs to read data does not exist, it is often difficult to make a direct judgment, but this information must be obtained through exceptions. The java. io. File class solves this problem well. Java. io. File class generally does not involve the specific content inside the File, but processes the File as a whole, such as obtaining various File information or deleting the File. Class java. io. File can operate not only files, but also paths.

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.