Java-based IO stream and javaio stream

Source: Internet
Author: User

Java-based IO stream and javaio stream

IO streams are used to process data transmission between devices. java operates on data through streams, and the objects of these operation streams are encapsulated in IO packages. Streams can be divided into two types: dense streams and word throttling. The encoding method can be set for the ghost stream, which makes it easier to process text.

IO common base classes

Byte stream abstraction base class InputStream, OutputStream. The extract stream abstraction base class Reader and Writer.

 

Middleware stream System Abstract base Writer

Common Methods: write (); flush (); close (); append ()

Subclass FileWriter

Used to write files. When an object of this type is initialized, the file to be operated must be specified. The write method writes data to the stream. The flush method refreshes the stream, closes and closes the stream.

Abstract class base class Reader

Common Methods: read (), skip (), reset (), ready ()

Subclass FileReader

Used to read files. When initializing an object, you must specify the file to be read. The read () method reads data into the stream.

 

IO Exception Handling IOException

 

 

 

 

 

The stream object fw should be declared outside the try block and initialized internally so that it can be accessed in finally. The close method can be called only when fw is not null, so judgment is required.

Shard Stream Buffer

The buffer of the upstream stream improves the IO efficiency and needs to be used in conjunction with the stream. The streaming function is enhanced based on the stream. The corresponding classes are BufferedReader and BufferedWriter.

BufferedWriter

Write text to the character output stream to buffer each character, so as to provide efficient writing of single characters, character arrays, and strings. Therefore, before creating a buffer, you only need to add the stream object to the constructor and then call the Writing Method of the buffer.

BufferedReader

Read text from the character input stream, buffer each character, so as to achieve efficient reading of characters, arrays, and rows.

LineNumberReader

The buffer character output stream of the trace row number. The setLineNumber and getlLineNumber methods can be used to set and obtain the current row number.

 

Byte stream System Abstract base class OutputStream

Common Methods: write (); flush (); close ();

Subclass FileOutputStream

Used to write files. When an object of this type is initialized, the file to be operated must be specified. Write method writes data to the stream, flush method refreshes the stream, close to refresh and close the stream

Abstract class base class InputStream

Common Methods: read (), skip (), reset (), ready ()

Subclass FileInputStream

Used to read files. When initializing an object, you must specify the file to be read. The read () method reads data into the stream. available () returns the number of bytes that can be read.

Byte stream buffer

The corresponding classes are BufferedOutputStream and BufferedInputStream.

BufferedOutputStream

This class implements buffered output streams. By setting this output stream, the application can write each byte to the underlying output stream without calling the underlying system for each byte write.

BufferedInputStream

The same as the upstream stream buffer.

 

Read the conversion stream

The InputStreamReader class exchanges byte streams into bytes streams. It specifies a character set to read bytes and decode them into characters. To achieve high efficiency, you can consider packing InputStreamReader in BufferedReader.

Write conversion stream

The OutputStreamWriter class converts a bytes stream to a byte stream. You can specify a character set to encode it into bytes. To achieve high efficiency, you can consider packaging OutputStreamWriter in BufferedWriter.

 

When the input stream is operated, the source ---- bridge (Rune conversion) ---- buffer should be clarified. The output stream operation should specify the purpose ------ bridge (Rune conversion) ------ buffer.

 

Flie class

It is used to encapsulate files or folders into objects to facilitate operations on files and folders. It can also be used as a parameter to pass to the stream constructor.

Common Methods:

  • Create
    • CreateNewFile (); create a new file. If yes, false is returned.
    • CreateTempFile (prefix, suffix) static method, create a temporary file based on the specified prefix suffix
    • Mkdir (); mkdirs (); create a directory
  • Delete
    • Delete (); delete an object
    • DeleteOnExit (); request to delete the file or directory at JVM termination.
  • Judgment

CanExcute (); canRead (); canWrite (); exist (); isDirectory (); isFile ()

  • Obtain information
    • Getname (); getPath (); getParent (); getAbsolutePath (); length ()
    • List (dir); get the file name array in the directory
    • ListFiles (dir); get the object Array

 

Properties class

This class is a collection container combined with I/O in a collection. It is a subclass of hashtable, that is, it has the characteristics of a map set and all key-value pairs are strings. This type of object can be used in configuration files in the form of key-value pairs.

  • SetProperties (str, str); getProperties (str) read Set Properties
  • Load () after loading the byte stream or the upstreaming stream version 1.6
  • Set <string> stringPropertyNames () returns the key Set in the property list.

 

Print stream byte output stream PrintStream class

As a subclass of outputStream, this function is added to other output streams so that they can print various numerical representations. Print all types of data as they are. You can directly operate the file. Its constructor accepts the file object, file path, and byte output stream.

Character output stream PrintWriter class

Its constructor can accept the file object, file path, byte output stream outputstream, and character output stream Writer.

SequenceInputStream

Combines multiple input streams into one input stream.

Object serialization

To access data objects in heap memory, you need to use the ObjectOutputStream and ObjectInputStream classes, also known as object persistence or object serialization.

Class to enable its serialization function by implementing the interface serializable. Static member variables in the class cannot be serialized, because static member variables are not stored in heap memory, but stored in the memory method area. If a non-static member is modified with the transient keyword, it can also be not serialized.

Pipeline Flow

Pipeline stream input and output can be directly connected. By using them in combination with threads, PipledOutputStream and PipledInputStream must be used.

The input stream of the MPs queue should be connected to the MPs queue output stream. The MPs queue input stream provides all the input bytes to be written to the MPs queue output stream. Generally, data is read by a thread from the PipledInputStream object and written to the corresponding PipledOutputStream object by other threads.

Random File Access

The RandomAccessFile class can perform random read/write operations on the file. It encapsulates byte arrays and operates on arrays through pointers. Get the pointer position through getFilePointer () and change the pointer position through seek.


For io streams in java

4. Basic byte streams
4.1. FileInputStream and FileOutputStream
These two node streams are used to manipulate disk files. Constructors of these classes allow you to specify the files they are connected.
To construct a FileInputStream, the associated file must exist and be readable.
If you want to construct a FileOutputStream and the output file already exists, it will be overwritten.
FileInputStream infile = new FileInputStream ("myfile. dat ");
FileOutputStream outfile = new FileOutputStream ("results. dat ");
4.1 BufferInputStream and BufferOutputStream
These are filter streams, which can improve the efficiency of I/O operations.
4.3. PipedInputStream and PipedOutputStream
The pipeline stream is used for communication between threads. The PipedInputStream object of one thread reads the input from the PipedOutputStream object of another thread.
To make the pipeline stream useful, you must have an input party and an output party.
4.4. DataInputStream and DataOutputStream
These filters read and write Basic Java classes through streams

5. Basic streams
The figure illustrates the architecture of Reader and Writer internal streams.
5.1. InputStreamReader and OutputStreamWriter
This interface is used to convert byte streams to byte streams.
When you construct an InputStreamReader or OutputStreamWriter, the conversion rules define the conversion between the 16-bit Unicode and the specific representation of other platforms.
InputStreamReader reads bytes from a data source and automatically converts them to Unicode characters.
If you specifically declare that InputStreamReade will replace byte streams with other types of bytes streams.
OutputStreamWriter writes the Unicode encoding of characters to the output stream. If you are not using Unicode characters, OutputStreamWriter converts your character encoding to Unicode encoding.
5.2. Buffer readers and authors
Because the conversion between different formats is similar to other I/O operations, the maximum efficiency of processing large data blocks is reached.
It is a good idea to link a BufferedReader and BufferedWriter at the end of InputStreamReader and OutputStreamWriter.
Remember to use the flush () method for BufferedWriter.
5.3 use other characters for conversion
If you need to read the input from a non-local character code (for example, from a network connection to a different type of machine,
You can use explicit character encoding to construct ir = new InputStreamReader (System. in, "8859_1 bytes) Like the following program 〃);
Note: If you read characters through a network connection, you should use this form.
Otherwise, your program will always try to convert the characters read as a local representation, which is not always correct. ISO 8859-1 is the Latin-1 encoding mode mapped to ASCII.

A question about the basic JAVA io stream

InputStreamReader converts a byte stream into a bytes stream. Therefore, BufferedReader can be used to buffer and receive messages.
The last generation of Reader names are all converted to the secondary stream.
ByteArrayInputStream is the real byte processing stream.

Related Article

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.