Detail of IO flow in Java

  • 2020-07-21 07:42:39
  • OfStack

Question:

1. Problems solved by IO

Input byte stream and output byte stream

3. Input character stream and output character stream

4. Role of conversion flow

5. Case code

1. IO solves problems

Solve data transfer problems between devices, such as hard disk -- > Memory memory - wat > The hard disk

2. Byte stream

Input byte stream

|InputStream all input byte streams base class, abstract class

The input byte stream for the file is read by |FileInputStream

The |BufferedInputStream buffered input byte stream maintains an 8KB byte array internally. The purpose of this class is to improve the efficiency of reading file data

Output byte stream

|OutputStream all output byte streams base class, abstract class

The output byte stream for the output data to a file

The | BufferedOutputStream buffered output byte stream is created to increase the efficiency of writing data to a file. The byte stream maintains 1 byte of 8KB

When byte stream is used: when data is read without being encoded or decoded, e.g. image data

3. Character stream

Character stream = byte stream + Encoding (decoding)

Input character stream

-|Reader all input character stream base class, abstract class

-- |FileReader reads the input character stream of file characters

|BufferedReader buffered input character stream, the purpose of this class is to improve the efficiency of reading file characters and expand the function (readLine()), it is to maintain an 8,192-length array

Output character stream

-|Writer all output character stream base class, abstract class

|FileWriter output character stream of character data to a file

|BufferedWriter buffered output character stream, this class is designed to improve the efficiency of writing file characters and expand the function (newLine()), it is actually internally maintained an 8,192-length array

When do we use character streams: If we are reading and writing character data, then we use character streams

4. Conversion flow

Input byte stream conversion: The input byte stream is converted to an input character stream

InputStreamReader

Output byte stream conversion stream

OutputStreamWriter

The role of the conversion flow

1. The corresponding byte stream can be converted into a character stream

2, can specify code table to read and write file data

FileReader FileWriter the default is to use the gbk code table, you can not specify the code table

5. Case code


package cn.xlucas.io;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
public class IoDemo1 {
 public static void main(String[] args) throws IOException {
  //testInput();
  writeFile();
  readFile();
 }
 // Read out the contents of the file 
 public static void readFile() throws IOException{
  // Create input data channels for files and programs 
  FileInputStream filein=new FileInputStream("F:\\a.txt");
  // Creates a transformation stream for the input byte stream and specifies the code table to read 
  InputStreamReader inputin=new InputStreamReader(filein,"utf-8");
  //FileReader filein =new FileReader("F:\\a.txt");// This is a messy situation  
  int context=0;
  while((context=inputin.read())!=-1){
   System.out.print((char)context);
  }
  // Close the resource 
  inputin.close();
  filein.close(); 
 }
 // The content with utf-8 Write the format to the file 
 public static void writeFile() throws IOException{
  // Created file and program data   channel 
  FileOutputStream fileout=new FileOutputStream("F:\\a.txt");
  // create 1 Two output byte stream conversion streams and specify code table to write data 
  OutputStreamWriter output=new OutputStreamWriter(fileout,"utf-8");
  output.write(" China ");
  output.close();
  fileout.close();
 }
 public static void testOutput() throws Exception{
  Socket socket = new Socket(InetAddress.getLocalHost(),9090);
  // Access to the socket Output stream object. 
  OutputStream outputStream = socket.getOutputStream();
  // Replace the output byte stream with the output character stream 
  OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
  outputStreamWriter.write(" Not hungry !");
 }
 public static void testInput() throws IOException{
  InputStream in=System.in;
  // Input conversion stream 
  InputStreamReader input=new InputStreamReader(in);
  BufferedReader buffer=new BufferedReader(input);// For buffering 
  //int context =in.read();//  It only gets read at a time 1 Three bytes of data 
  System.out.print(buffer.readLine());//读取1 Rows of data 
  // Close the word 
  buffer.close();
  input.close();
 }
}

Above is the content of this article, the need of friends can refer to below


Related articles: