Implementing object-oriented-file I/O using Java

Source: Internet
Author: User
Tags object serialization

The Java.io.File class is used to represent files (directories)

The file class is used only for information (name, size, etc.) that represent files (directories) and cannot be used for access to file content

Randomaccessfile Java provides access to the contents of a file that can be read or written.

Randomaccessfile supports random access to files and can access any location in the file

1.java file Model

The file on the hard disk is stored in byte byte byte and is a collection of data

2. Open File

There are two modes of "RW" (read-write) "R" (read-only)

Randomaccessfile RAF = new Randomeaccessfile (file, "RW")

The file pointer opens the file when the pointer is at the beginning pointer = 0;

3. Writing methods

Raf.write (int)---> Write only one byte (last 8 bits), while the pointer points to the next position, ready to write again

4. Reading methods

int b = raf.read ()---> read one byte

5. Must be closed after the file is read and written (Oracle official note)

Serialization and serialization of basic types

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

Data---->n byte

2) deserialization

The process of converting n bytes into a single data

Nbyte---> Data

3) Randomaccessfile provides basic types of read and write methods that can be used to base type data

Serialize to a file or deserialize the contents of a file into data

IO stream (input stream, output stream)

Byte stream, character stream

1. Byte stream

1) InputStream, OutputStream

InputStream abstracts the way the application reads data

OutputStream abstracts the way the application writes data

2) EOF = end Read-1 reading to the end

3) Input Stream Basic method

int b = In.read (); reads a byte unsigned fill to int low eight bits. -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 out a byte to stream, 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---> specifically read the data on the file

6) FileOutputStream implements a method for writing byte data to a file

Second, character stream

1) Coding issues

2) Recognize text and text files

Java text (char) is a 16-bit unsigned integer, which is the Unicode encoding of the character (double-byte encoding)

File is a byte of byte byte ... The data series

A text file is a stored result of a sequence of text (char) serialized into Byte according to an encoding scheme (UTF-8,UTF-16BE,GBK)

3) Character stream (Reader Writer)----> operation is text text file

Processing of characters, one character at a time

The bottom of the character is still the basic byte sequence

Basic implementation of character stream

InputStreamReader completes a byte stream parsing to a char stream, parsed by encoding

OutputStreamWriter provides a char stream to a byte stream, which is processed by encoding

Filereader/filewriter

Filter for character streams

BufferedReader---->readline read one line at a time

Bufferedwriter/printwriter----> Write a line

III. serialization of objects and deserialization

1) object serialization, that is, the object is converted to a byte sequence, the inverse is called the deserialization of objects

2) serialized Stream (ObjectOutputStream), is the filter stream----writeobject

Deserialization stream (ObjectInputStream)---readobject

3) serialization Interface (Serializable)

Object must implement a serialization interface for serialization, or an exception will occur

This interface, without any method, is just a standard

4) Transient keywords

private void WriteObject (Java.io.ObjectOutputStream s)

Throws Java.io.IOException

private void ReadObject (Java.io.ObjectInputStream s)

Throws Java.io.IOException, ClassNotFoundException

Analyzing the problems of serialization and deserialization in ArrayList source code

5) Serialization of the call problem for the neutron class and the parent class constructor

The deserialization of a subclass object is that if there is a parent class that does not implement a serialization interface, the constructor of its parent class is called.

Implementing object-oriented-file I/O using Java

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.