Dark Horse programmer-other classes in the IO package and other classes in the io package

Source: Internet
Author: User

Dark Horse programmer-other classes in the IO package and other classes in the io package

Other classes in the IO package:

1. Print stream: provides a printing method to print data of various data types as is
Byte print stream PrintStream
Acceptable parameter types of constructors:
(1) File object
(2) String path String
(3) Output byte stream OutputStream

Character print stream PrintWriter
Acceptable parameter types of constructors:
(1) File object
(2) String path String
(3) Output byte stream OutputStream
(4) character output stream: Writer
2. SequenceInputStream (tandem Stream)

It is used to write data from multiple streams into one stream. The constructor passes in Enumeration <? Extends InpuStream>
Vector is required
3. Operation object
ObjectInputStream, ObjectOutputStream
The operated object must implement Serializable (Mark interface ),
When Serializable is running, a version number called serialVersionUID is associated with each Serializable class,
The minor version number is calculated based on the Members in the class .. If the serialVersionUID of the class loaded by the recipient is different from the version number of the corresponding sender
Deserialization will cause InvalidClassException. The serializable class can declare a field named serialVersion (This field must be static and the final long field)
Explicitly declare your own serialVersion so that deserialization can be performed when class members are changed.

Static members are not serialized, because only the data in the pair can be serialized, and the static members are in the method area.
If you want to touch a member that is not serialized, you can modify it with transient to ensure that its value exists in the memory instead of in the text file.
Read objects and persistent objects using the readObject and writeObject methods respectively.

4. PipedInputStream and PipedOutputStream
The input stream of the pipeline should be connected to the output stream of the pipeline. The input stream of the pipeline provides all data bytes to be written to the output stream of the pipeline.
Generally, data is read by a thread from the PipedInputStream object and written to the corresponding PipedOutputStream by other threads.
We do not recommend that you try to use a single thread for these two objects, because the thread may be deadlocked.
. The input stream of the MPs queue contains a buffer. You can separate read and write operations within the buffer range.
If the thread that provides data bytes to the output stream of the connection pipe no longer exists, the pipe is damaged.
5. RandomAccessFile:
This class is not a subclass of the stream system, but directly inherited from the Object
But he is a member of the IO package, because it has the read/write function.
An array is encapsulated internally, and elements of the array are operated through pointers.
You can use getFilePointer to obtain the pointer position.
Colleagues can use seek to change the pointer position.
In fact, the principle of reading and writing is that the byte input stream and the output stream are encapsulated internally.
Through its constructor, we can see that this class can only operate files, and the operation price also has a mode:

6. Basic data types of operations
DataInputStream, DataOutputStream
7. Operate byte Arrays: Use the stream's read/write idea to operate arrays.
ByteArrayInputStream: when constructing a data source, the data source must be a byte array.
ByteArrayOutputStream: you do not need to define the data destination during construction, because the object contains a byte array of variable length.
This is the data destination

Because both stream objects operate on arrays and do not use system resources, do not close them.

8. operational character array: CharArrayReader, CharArrayWriter
9. Convert character stream objects into byte stream objects and use the conversion stream: InputStreamReader (the bridge between byte and character)
InputStreamReader isr = new InputStreamReader (new FileInputStream ("gbk.txt"), "gbk ")
OutputStreamWriter (the bridge between character and byte)
OutputStreamReader osr = new OutputStreamReader (new FileOutputStream ("utf.txt"), "UTF-8 ")

10. encoding: String to a byte array String> String new String (byte [], charsetName)
The Tomcat encoding method is iso8859-1
If you use gbk encoding to store the data, the decoding is iso8859-1,
In this case, the decoded data can be encoded by iso8859-1 to obtain the original byte data, and then decoded by gbk.
However, if garbled characters are decoded with UTF-8 at the beginning of the term, they are irreversible because both gbk and UTF-8 recognize Chinese characters.

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.