Teach yourself JAVA-11: IO streams

1. The flow is divided into two directions: input flow and output flow. (with memory as a reference)
When reading data from a data source into memory, it is called an input stream or a read stream.
When data in memory is written to a data source, it is called an output stream or a write stream.

2. The stream is divided according to the content of transmission: byte stream, character stream and object stream.
No matter what kind of stream the bottom part is transmitted in bytes. Therefore, its essence is a byte stream. But in order to facilitate programmers to better manipulate character data and object data. Therefore, a layer of packaging is made on the basis of the byte stream to form a character stream and an object stream.

Byte stream: abstract parent classes are InputStream and OutputStream
Character stream: abstract parent classes are Reader and Writer

3. The steps of stream operation:
①Create flow
②Operate flow
③Close flow

When manipulating files, if the file does not exist, the read stream will throw a file not found exception, while the write stream will create a new file.

The main purpose of stream operations: transformation of data.

4. Serialization:
When the object needs to be transmitted, because the data in the object is very large, it cannot be transmitted directly. Then, before transmission, the object needs to be broken up into a binary sequence for transmission. This process is called the serialization process; after reaching the destination, the binary sequence is restored into an object, which is called the deserialization process.

All objects that need to implement object serialization must first implement the Serializable interface.

java.io.NotSerializableException: Thrown when an object needs to be transferred, and the class where the object is located does not implement the serialization interface.

transient keyword: modifier for properties. Indicates that the attribute value modified by transized will not be transmitted when the object is transmitted.

 

byte stream:

/** Read stream*/
public void readData() {
InputStream in = null;
  try {
  // Create file read byte stream
  in = new FileInputStream("//Read file address");
  int data = 0;

  // read() reads one byte at a time and returns the read data. When the return value is -1, it means the file is read
   while ((data = in.read()) != -1) {
     System. out.println(data);
    }
  byte[] by = new byte[1024];
  int len ​​= 0;
  // Read the data in the stream into a byte array and return the number of bytes currently read. After reading, return -1
  while ((len = in.read(by)) != -1) {
  System.out.println(len);
  }

} catch (Exception e) {
e.printStackTrace();
} finally {
  try {
  // 关闭流
  in.close();
} catch (IOException e) {
  e.printStackTrace();
    }
}

/** Write stream*/
public void writeData(String str) {
  OutputStream out = null;
try {
  // true means write data in append mode
  out = new FileOutputStream("//write address", true);

  // Convert string to byte array
  out.write(str.getBytes());
} catch (Exception e) {
  e.printStackTrace();
} finally {
try {
  out.close();
} catch (IOException e ) {
  e.printStackTrace();
  }
}


/**Copy file*/
public void copyFile() {
  InputStream in = null;
  OutputStream out = null;
  try {
  in = new FileInputStream("//Read address");
  out = new FileOutputStream("//write address");
  byte[] by = new byte[1024];
  int len ​​= 0;
  while ((len = in.read(by)) != -1) {
  //read Take a few bytes, write a few bytes
  out.write(by,0,len);
}
} catch (Exception e) {
  e.printStackTrace();
} finally {
try {
out.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

 

Character stream:
/**read*/
public void readData() {
Reader r = null;
BufferedReader br = null;

try {
//Create a file read stream
r = new FileReader("//Read address");
//Socket stream, on the basis of a stream, socket another stream, also known as advanced stream
br = new BufferedReader(r);
String str = null;
//Read a line of data, ending with \n or carriage return
while ((str = br.readLine()) != null) {

System.out.println(str) ;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
br.close();
r.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

/**写入*/
public void writerData(String str){
Writer w =null;
try {
w=new FileWriter("//写入地址", true);
w.write(str);
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
w.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325112541&siteId=291194637