Other classes in the Java Foundation 7:I/O Package

Source: Internet
Author: User


On the Java foundation of the article, I think it can also be written in my other blog, is definitely original, and now share to everyone out.

--------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------- ---------



I. Object Flow (Objectinputstream/objectoutputstream) 1, overview

The objects in the heap memory are stored in the hard disk, preserving the data in the object, known as the Persistence (or serialization) of the object. Two classes used: ObjectInputStream and ObjectOutputStream

The object to be manipulated needs to be implemented Serializable

2. Features

Object serialization and deserialization. -----------writeobject  readobject

Serializable Tagged interface ------ --------------

Keyword: Transient------------------------ short, static and transient objects are not written to

Transient: Non-static data does not want to be serialized and can be decorated with this keyword.

3. For example:

public static void Readobj () throws IOException, classnotfoundexception {objectinputstream ois = new ObjectInputStream (NE W FileInputStream ("Obj.object"));//deserialization of the object. Person P = (person) ois.readobject (); System.out.println (P.getname () + ":" +p.getage ()); Ois.close (); public static void Writeobj () throws IOException, IOException {objectoutputstream oos = new ObjectOutputStream (New Fileout Putstream ("Obj.object"));//Object serialization.  the serialized object must implement the Serializable interface. Oos.writeobject (New Person ("Xiao Qiang"); Oos.close ();}

Second, Randomaccessfile random access files:I. OverviewRandomaccessfile instances of this class support reading and writing of random access files, and they have read and write methods.


2. Features:

* * can be read and write.

* * The internal maintenance of a large byte array, through the operation of the array to complete the read and write.

* * The position of the pointer is obtained through the Getfilepointer method, and the position of the pointer can also be set through the Seek method.

* * The object's contents should encapsulate the byte input stream and the byte output stream.

* * This object can only manipulate files.

You can read and write from anywhere in the array by using the Seek method to manipulate the pointer.

You can complete the modification of the data.

However, be aware that data must be regular.




3. For example:

Defines a random access file, readable writable randomaccessfile RAF = new Randomaccessfile ("Ranacc.txt", "RW"), Raf.write ("Zhang San". GetBytes ());// Write content Raf.writeint (<span), style= "White-space:pre" ></span>//to set the position of the pointer through seek. <span style= "White-space:pre" ></span>randomaccessfile RAF = new Randomaccessfile ("Ranacc.txt", "R"); Raf.seek (1*8);//Random read. Just specify the position of the pointer. byte[] buf = new Byte[4];raf.read (BUF);

Three, Pipeline flow: 1, overview

Pipeline flow including PipedOutputStream and PipedInputStream, and the need and multithreading technology combination, do not know the multi-threaded knowledge whether you still remember. Look at examples to recall.

2. Example


Iv. DataInputStream and Bytearrayinputstream

1. An object that operates on a value of the base data type.

DataInputStream

DataOutputStream

These two read and write objects, which can be used to manipulate the stream object of the base data type, contain methods for reading and writing various basic data types.


2. The device is a stream object of memory.

Bytearrayinputstream Bytearrayoutputstream

CharArrayReader Chararraywriter

A, this object does not invoke the underlying resource, so you do not have to close the stream resource, even after it is closed, it can still be called.

b, the internal contains a buffer, the equivalent of memory as the source and purpose of the stream operation, no IO exception is generated.

C, write a number to another array (all in memory operation)



V. Last

This blog content overall is not too difficult, but trivial, blog introduction is not very detailed, first of all about the knowledge, and so on when the real use of the API can be checked documents.


Other classes in the Java Foundation 7:I/O package

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.