Java Base IO Stream

Source: Internet
Author: User

IO streams are used to process inter-device data transfers, and Java's operations are streamed through streams, and the objects of these flow of operations are encapsulated in IO packets. Streams can be divided into character stream and byte stream. Character streams can be encoded, which makes it easier to work with text.

IO Common base class

Byte stream abstract base class Inputstream,outputstream. The character stream abstract base class Reader,writer.

?

Character Stream system

Abstract base class writer

Common methods: Write (); flush (); close (); append ()

Sub-class FileWriter

Used to write to a file. When an object of this class is initialized, it specifies the file to be manipulated. The Write method writes data to the stream, the Flush method refreshes the stream, and close refreshes and closes the stream.

Abstract class base class Reader

Common methods: Read (); skip (); reset (); Ready ()

Sub-class FileReader

Used to read files. Specifies the file to be read when the object is initialized. The Read () method reads the data into the stream.

?

Io exception Handling IOException

?

The Stream object FW is to be placed outside the try block declaration, initialized internally so that it can be accessed in finally, and only if the FW is not NULL to call the Close method, it needs to be judged.

Buffer of character stream

The buffer of character stream improves the efficiency of IO, it needs to be used in combination with flow, and the function of convection is enhanced on the basis of stream. The corresponding classes are BufferedReader and BufferedWriter.

BufferedWriter

Writes text to the character output stream, buffering individual characters, providing a single character, an array of characters, and efficient writing of strings. Therefore, before creating the buffer to the existing stream object, as long as the stream object is added to the constructor, and then call the buffer write method.

BufferedReader

Reads text from the character input stream, buffers individual characters, and implements efficient readout of characters, arrays, and rows.

Sub-class LineNumberReader

A buffered character output stream that tracks line numbers, which can be set and obtained by the Setlinenumber and Getllinenumber methods.

?

BYTE stream system

Abstract base class OutputStream

Common methods: Write (); flush (); close ();

Sub-class FileOutputStream

Used to write to a file. When an object of this class is initialized, it specifies the file to be manipulated. The Write method writes data to the stream, flush method flushes the stream, and close refreshes and closes the stream

Abstract class base class InputStream

Common methods: Read (); skip (); reset (); Ready ()

Sub-class FileInputStream

Used to read files. Specifies the file to be read when the object is initialized. The Read () method reads the data into the stream and available () returns the number of bytes that can be read.

Buffer of Byte stream

The corresponding classes are Bufferedoutputstream and Bufferedinputstream

Bufferedoutputstream

This class implements a buffered output stream, which, by setting this output stream, allows the application to write individual bytes to the underlying output stream without having to call the underlying system for each byte write.

Bufferedinputstream

The same character stream buffer is the same.

?

Read the Transformation stream

The InputStreamReader class converts a byte stream into a character stream. It specifies a character set to read bytes and decode into characters. In order to achieve high efficiency, it is possible to consider packaging InputStreamReader within BufferedReader.

Write conversion stream

The OutputStreamWriter class converts a character stream to a byte stream. You can specify a character set to encode into bytes. In order to achieve high efficiency, it is possible to consider packaging outputstreamwriter within BufferedWriter.

?

Input stream operation should be clear source----bridge (abstracted conversion)----buffer, the output stream operation should be clear purpose------bridge (abstracted conversion)------buffer.

?

Flie class

Used to encapsulate files or folders into objects for easy operation on files and folders, or as arguments to a stream's constructor.

Common methods:

    • Create
      • CreateNewFile (); Creates a new file, returns False if present
      • Createtempfile (Prefix,suffix) static method to create a temporary file based on the specified prefix suffix
      • mkdir (); mkdirs (); Creating a Directory
    • Delete
      • Delete (); Remove file
      • Deleteonexit (); Request to delete this file or directory when the JVM terminates.
    • Judge

Canexcute (); CanRead (); CanWrite (); exist (); Isdirectory (); Isfile ()

    • Get information
      • GetName (); GetPath (); GetParent (); GetAbsolutePath (); Length ()
      • List (dir); get an array of file names under directory
      • Listfiles (dir); get an array of file objects

?

Properties Class

The class is a collection container in the collection that is combined with IO, which is a subclass of the Hashtable, which is the feature of the Map collection, and the key-value pairs are strings. The class object can be used for configuration files in the form of key-value pairs.

    • SetProperties (STR,STR); getProperties (str) Read Settings properties
    • Load () after loading the byte stream or character stream 1.6 version
    • Set<string> Stringpropertynames () returns the collection of keys in the property list

?

Print Flow

Byte output stream PrintStream class

As subclasses of OutputStream, features are added to other output streams so that they can print various numeric representations. Print all the data as it is. You can manipulate the file directly, and its constructor accepts the file object, path, and byte output stream.

Character output stream PrintWriter class

Its constructor can accept the file object, path, byte output stream outputstream, character output stream writer

Sequenceinputstream

Merges multiple input streams into one input stream.

Serialization of objects

Implementing access to data objects in heap memory requires the ObjectOutputStream and ObjectInputStream classes to be implemented, also known as Object persistence or object serialization.

The Serializable class enables its serialization functionality by implementing an interface. Static member variables in a class cannot be serialized because static member variables are not stored in heap memory, but are stored in the memory method area. Non-static members can also be serialized without the addition of the transient keyword modifier.

Pipe flow

Through the pipeline flow input and output can be directly connected, by combining thread use, need to use the Pipledoutputstream and Pipledinputstream class to achieve.

The pipeline stream should be connected to the pipeline output flow, and the pipeline input stream provides all the inputs to be written to the pipeline output streams. Typically, data is read from a Pipledinputstream object by a thread and written to the corresponding Pipledoutputstream object by another thread.

Random access to files

The object of the Randomaccessfile class can read and write random files, and internally encapsulates a byte array, which is manipulated by a pointer array. Gets the pointer position through Getfilepointer () and changes the pointer position by seek.

Java Base IO Stream

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.