Use java to implement object-oriented-File I/O, object-oriented-file

Source: Internet
Author: User

Use java to implement object-oriented-File I/O, object-oriented-file

The java. io. File class is used to indicate files (directories)

The File class is only used to indicate the information (name, size, etc.) of a File (directory) and cannot be used to access the File content.

 

RandomAccessFile java provides access to the file content, which can read or write files.

RandomAccessFile supports random access to files and can access any location of Files

 

1. java file Model

Files on the hard disk are stored in byte bytes and are a collection of data.

2. open the file

There are two modes: "rw" (read/write) "r" (read-only)

RandomAccessFile raf = new RandomeAccessFile (file, "rw ")

File pointer. When opening a file, the pointer starts with pointer = 0;

3. Write Method

Raf. write (int) ---> write only one byte (the last 8 bits), point the pointer to the next position, and write again

4. Read Method

Int B = raf. read () ---> read a byte

5. Be sure to close the file after reading/writing is completed (Oracle official instructions)

 

 

I. serialization and basic type serialization

1) the process of converting the int type to 4 bytes or converting other data types to byte is called serialization.

Data ----> n byte

2) deserialization

The process of converting n Bytes into one data

Nbyte ---> data

3) RandomAccessFile provides the basic types of read/write methods.

Serialize to a file or deserialize the file content into data

IO stream (input stream and output stream)

Byte stream and byte stream

1. byte stream

1) InputStream and OutputStream

InputStream abstracts how an application reads data.

OutputStream abstracts the way an application writes data.

2) EOF = End read-1 read to the End

3) basic input stream method

Int B = in. read (); read a byte unsigned to int low octal.-1 is EOF

In. read (byte [] buf)

In. read (byte [] buf, int start, int size)

4) basic method of output stream

Out. write (int B) writes a byte to the stream, and B's low 8 bits

Out. write (byte [] buf) writes the buf byte array to the stream

Out. write (byte [] buf, int start, int size)

 

5) FileInputStream ---> implements Data Reading on the file.

6) FileOutputStream implements writing byte data to the file.

Ii. Ghost stream

1) Encoding Problems

2) recognize text and text files

Java text (char) is a 16-bit unsigned integer and unicode (dubyte encoding)

The file is a data sequence of byte...

A text file is a storage result of serialized text (char) sequences into byte according to a certain encoding scheme (UTF-8, utf-16be, gbk)

3) Reader Writer ----> operates on text files.

Character Processing, processing one character at a time

The bottom layer of the character is the basic byte sequence.

Basic implementation of a stream

InputStreamReader parses byte streams into char streams and parses them according to Encoding

OutputStreamWriter provides char stream to byte stream, which is encoded

FileReader/FileWriter

Filters of the upstream stream

BufferedReader ----> readLine reads a row at a time

BufferedWriter/PrintWriter ----> write a row

Iii. Object serialization and deserialization

1) Object serialization means to convert an Object into a byte sequence, and vice versa.

2) The serialized stream (ObjectOutputStream) is a filter stream ---- writeObject

Deserialization stream (ObjectInputStream) --- readObject

 

3) Serializable)

The object must implement the serialization interface before it can be serialized. Otherwise, an exception occurs.

This interface does not have any method, just a standard

 

4) transient keyword

Private void writeObject (java. io. ObjectOutputStream s)

Throws java. io. IOException

Private void readObject (java. io. ObjectInputStream s)

Throws java. io. IOException, ClassNotFoundException

Analyze serialization and deserialization issues in ArrayList source code

 

5) Call of subclass and parent class constructor in serialization

Deserialization is performed on subclass objects. If a parent class does not implement a serialization interface, the constructor of its parent class is called.

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.