JAVA learning notes-Basic JAVA syntax (8)

Source: Internet
Author: User

I. What is I/O (Input/Output)

A program is running in the memory, and I/o is a similar concept similar to a pipeline in which nodes outside the memory of the JAVA program transmit data to each other.

Ii. Stream

1. concept: the channel used for data exchange between JAVA and other nodes is equivalent to a pipe connecting two nodes for data transmission.

2. Category:

  1) divide by direction: Input stream and output stream

Some people may be confused about the input and output. In what circumstances is input and output? Let me give you an example: JAVA creates a memory (JAVA memory) in the memory of our computer, and another node is, for example, a file (hard disk) in our hard disk ), when the data in the JAVA memory is input to the hard disk, it is called the output. On the contrary, the hard disk reads the data into the JAVA memory and it is called the input. That is to say, the Input and Output are relative to those in JAVA. If you add data to the JAVA memory, the Input is used. Otherwise, the Output is used)

  2) According to the content:

  Byte stream: One byte and one byte for transmission. If it is used to transmit Chinese characters, the Chinese character will be split into two bytes for transmission one by one.

  Ghost stream: One character and one character transmission (one character in JAVA is 2 bytes)

Knowledge Extension: JAVA streams are inherited from four abstract classes: InputStream/OutputStream (byte stream) Reader/Writer (Bytes stream) and InputStream and Reader. The functions of OutputStream and Writer are similar.

For example, the relationship between them is illustrated in detail and the image.

 

3) by function:

Node stream: The two ends of the stream are directly connected to the node. The function is used to transmit data between two nodes.

The following applet implements copying between objects:

Import java. io .*;

Public class test {

Public static void main (String [] args) throws Exception {

FileInputStream is = new FileInputStream ("e:/123.txt ");

FileOutputStream OS = new FileOutputStream ("e:/abc.txt ");

Int n =-1;

While (n = is. read ())! =-1 ){

OS. write (char) n );

}

Is. close ();

OS. close ();

}

}

Note:1. When JAVA writes data to the outside, if the stream is closed and data is written to the file, it overwrites the data. If it is closed, it appends the data.

2. As long as JAVA establishes a connection with other content (I/O, database connection, socket connection), the first time consumed, and the second time consumed resources, you must disable it when you use it.

3. the byte stream uses the ISO-8859-1 encoding; the character stream uses the unicode encoding (UTF-8) So if the transmitted data is a character, you must use the character stream, as shown above, modify FileInputStream is = new FileInputStream ("e:/123.txt"); and FileOutputStream OS = new FileOutputStream (" e:/abc.txt "): fileReader is = new FileReader ("e:/123.txt"); and FileWriter OS = new FileWriter (" e:/abc.txt ");

 

Stream processing:It cannot be directly connected to a node, but can only be mounted to a node stream. Adds additional functions to the node stream. Stream processing cannot be used independently (add functions outside the node stream)

Buffer stream: Adds a buffer to the node stream.

Buffer zone:The hard disk's loss rate is related to the number of reads and writes on the hard disk. When the buffer zone is not used, data written to the hard disk is written in one byte. If the buffer zone is used, the default size is 8 Mb, when the buffer zone is full, it is automatically written to the hard disk to protect the hard disk.

  Two Functions of Flush (): Forcibly write data in the buffer zone to the file; refresh and clear the Buffer Zone

Conversion stream:Change byte stream to byte stream, so it can only be set in byte stream (basically not very useful)

 

3. Date (Data Stream ):

Only characters and bytes can be transmitted in the stream. The data type of JAVA cannot be transmitted, but the data stream can specify the Data Type of JAVA to be transmitted, and the data stream is also nested in the transmission pipeline between JAVA memory and hard disk. It can be UTF or Int type, and these are all JAVA's own knowledge. In our opinion, they are some encodings, but for JAVA, it can fully identify what data type it is. This data stream is often used in future programs.

Iv. Object stream:

   Function: New feature serialization of JAVA5.0, which is used to read objects

  Serialization:Write the JAVA object. It is similar to persistence.

 Deserialization:Encapsulate read data into JAVA objects

When you use Object stream to save an Object, you must implement the Serializable serialization interface in the class of the Object to be saved. However, if you do not want to serialize a property in the object, add the transient keyword.

The following program describes how to use an Object stream:

 1 import java.io.*; 2  3 public class test{ 4  5          public static void main(String[] args) throws Exception{ 6  7                    //ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("e:/abc.txt")); 8  9                    //oos.writeObject(new Person("jack","23","boy","student"));10 11                    //oos.close();           12 13                    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("e:/abc.txt"));14 15                    Person p = (Person)ois.readObject();16 17                    System.out.println(p.name);18 19                    System.out.println(p.age);20 21                    System.out.println(p.sex);22 23                    System.out.println(p.job);24          }       25 }26 27 class Person implements Serializable{28 29          String name;30 31          String age;32 33          transient String sex;34 35          String job;36 37          Person(String name,String age,String sex,String job){38 39                    this.name = name;40 41                    this.age = age;42 43                    this.sex = sex;44 45                    this.job = job;46 47          }48 49 }

 

 

Related Article

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.