java object oriented 2 (26) native IO details

Native IO

I. overview

Flow description

Class name

File stream

FileInputStream,FileOutputStream,FileReader,FileWriter

Buffer flow

BufferedInputStream,BufferedOutputStream,BufferedReader,BufferedWriter

Object flow

ObjectInputStream,ObjectOutputStream

Converted flow

InputStreamReader,OutputStreamWriter

Printing flow

PrintStream

FileUtils: it's not brought by Java. It's written by others. We need to use the import package

IO tool in Java, used to operate hard disk

 

 

2, File flow (review)

For reading and writing files

 

  • Byte stream: byte. Read and write all files (text, video, pictures, music ).

  • Character stream: String special read-write text file

 

To write data:

FileOutputStream output byte stream

FileWriter output character stream

 

/*
*Review - file flow - write data
* */
public static void main(String[] args) throws Exception {
/ / 1. Byte flow to write out data: output byte flow (operate all data)
/ / 1.1 create object
FileOutputStream fos = new FileOutputStream("test byte stream. txt");
/ / 1.2 write data out
fos.write("little Liu is handsome today". getBytes("utf-8"));
/ / 1.3 closure
    fos.close();

/ / 2. Character flow out write data: output character flow (professional write text)
/ / 2.1 create object
FileWriter w = new FileWriter("test character stream. txt");
/ / 2.2 write data out
w.write("Xiaoyan is really handsome today");
/ / 2.3 closure
    w.close();
}

 

To read data:

FileInputStream input byte stream

FileReader input character stream

/*
*Review - file flow - read data
* */
public static void main(String[] args) throws Exception {
/ / 1. Byte stream read data: input byte stream
/ / 1.1 create object
FileInputStream fis = new FileInputStream("test byte stream. txt");
/ / 1.2 read data (byte data)
/ / method 1:
    /*byte b1 = (byte) fis.read();
    byte b2 = (byte) fis.read();
    byte b3 = (byte) fis.read();
    byte[] arr = {b1,b2,b3};
    String str = new String(arr, "utf-8");
    System.out.println(str);*/
/ / mode 2: read one byte array at a time
    byte[] arr = new byte[1024];
    fis.read(arr);
    String str = new String(arr, "utf-8");
    System.out.println(str);
/ / 1.3 closure
    fis.close();

/ / 2. Character stream read data: input character stream
/ / 2.1 create object
FileReader reader = new FileReader("test character stream. txt");
/ / 2.2 read data
    System.out.println((char) reader.read());
    System.out.println((char) reader.read());
    System.out.println((char) reader.read());
    System.out.println((char) reader.read());
    System.out.println((char) reader.read());
    System.out.println((char) reader.read());
    System.out.println((char) reader.read());
/ / 2.3 closure
    reader.close();
}

 

Operating text file: character stream (convenient, more efficient with buffer)

Buffer needs to be refreshed (buffer data can only be written to files after being refreshed)

  • Data volume reaches 8K

  • Call method: close();

  • Call method: flush();

 

Manipulating other files: byte streams

 

 

3, Buffer stream

To improve the efficiency of file stream reading and writing

Buffer stream is just a package on the file stream, which will not change the original file stream function and enhance (read-write efficiency)
Buffered buffer
BufferedInputStream, BufferedOutputStream, BufferedReader and BufferedWriter are the buffer streams used in method usage and
FileInputStream, FileOutputStream, FileReader and FileWriter are no different, but the buffer is increased.


FileReader and BufferedReader:
FileReader reads one character at a time
BufferedReader reads one line of characters at a time

 

  • BufferedReader: read one line at a time

  • 
    /*
    * Buffer stream: buffer character input stream
    * */
    public static void main(String[] args) throws Exception {
        //1. Create file stream (buy Mobile)
        FileReader f = new FileReader("Test character stream.txt");
        //2. Create buffer stream, package file stream (buy mobile phone shell, package mobile phone)
        BufferedReader br = new BufferedReader(f);
        //3. Use buffer stream to read data
        System.out.println(br.readLine());
        System.out.println(br.readLine());
        System.out.println(br.readLine());
        System.out.println(br.readLine());//If there is no data, null will be returned
        //4. Turn off (backward)
        br.close();
        f.close();
    }

Summary:

Buffer stream will not change the function of the original file stream, only enhance it.

Routine steps:

//1. Create file stream (buy Mobile)

//2. Create buffer stream, package file stream (buy mobile phone shell, package mobile phone)

//3. Use buffer stream (for mobile phones with mobile phone shell)

//4. To close (backward) a buffer stream before a file stream

 

 

4, Object flow

For reading and writing objects

The serialization stream has two operations:

  • Serialization (writing out data): ObjectOutputStream

  • Deserialization (reading data): ObjectInputStream

 

The premise of the above two operations: the class of the operated object implements the java.io.Serializable interface

 

 

Person Class:
public class Person implements Serializable{
    private String name;
    private String sex;
    private int age;
}

//Test class:
public static void main(String[] args) throws Exception {
    //1. Write data
    //1.1 create file stream
    FileOutputStream fos = new FileOutputStream("person.ser");
    //1.2 create object stream and package file stream
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    //1.3 write out object data
    oos.writeObject(new Person("Smile","male",18));
    //1.4, Guan Liu
    oos.close();
    fos.close();
    //2. Read data
    //2.1 create file stream
    FileInputStream fis = new FileInputStream("person.ser");
    //2.2 create object stream and package file stream
    ObjectInputStream ois = new ObjectInputStream(fis);
    //2.3 read object data
    Object obj = ois.readObject();
    Person p = (Person) obj;
    System.out.println(p);
    //2.4, Guan Liu
    ois.close();
    fis.close();
}

 

Summary:

Object streams can write out read object data.

If the object of a class needs to be read and written through the object stream, the class needs to implement an interface: java.io.Serializable

 

 

5, Transform flow (understanding)

Used for byte character conversion

Byte stream is universal, but in some cases, we need to use byte stream as character stream, and then we need to convert stream

Conversion stream: InputStreamReader, OutputStreamWriter

/*
* Conversion flow:
*   Byte ----- character
* */
public static void main(String[] args) throws Exception {
    //1. Output byte stream ----- output character stream
    //1.1. Create byte stream
    FileOutputStream fos = new FileOutputStream("Test conversion flow.txt");
    //1.2 create conversion stream, convert byte stream to character stream
    OutputStreamWriter w = new OutputStreamWriter(fos);
    //1.3 normal use of converted character stream
    w.write("Xiao He-It's just a sharp corner");
    //1.4 closure (reverse closure)
    w.close();
    fos.close();
    //2. Input byte stream ----- input character stream
    //2.1. Create byte stream
    FileInputStream fis = new FileInputStream("Test conversion flow.txt");
    //2.2 create conversion stream, convert byte stream to character stream
    InputStreamReader ir = new InputStreamReader(fis);
    //2.3 normal use of converted character stream
    System.out.println((char)ir.read());
    //2.4, Guan Liu
    ir.close();
    fis.close();
}

 

Summary: conversion stream cannot process character = = = 'byte

 

 

 

6, Print stream

Used to print text information quickly

/*
* Printing flow
* */
public static void main(String[] args) throws Exception {
    //1. Create a print stream
    PrintWriter pw = new PrintWriter("Test print stream.txt", "utf-8");
    //2. Print text data to a file
    pw.println("Hello");
    pw.println("Now 11:33");
    pw.println("it's a nice day today");
    pw.print("What do you have for lunch today?");
    pw.print("How about lobster");
    //3, Guan Liu
    pw.close();
}

 

 

 

7, Native IO - a complete way to add exception handling mechanism try catch finally

Preparation Code:
/*
* Throw exception
* */
public static void main(String[] args) throws Exception {
    //1.1 creating objects
    FileWriter w = new FileWriter("Test character stream.txt");
    //1.2 write data out
    w.write("I'm so handsome today");
    //1.3, Guan Liu
    w.close();
}

How to try... catch. . . finally

 

 

 

/*
* Standard writing method of flow: quickly generated through modification
* */
public static void main(String[] args) {
    FileWriter w = null;
    try {
        //1.1 creating objects
        w = new FileWriter("Test character stream.txt");
        //1.2 write data out
        w.write("I'm so handsome today");

    } catch (IOException e) {
        e.printStackTrace();//Print exception information to the console (exception name, exception code location)
    } finally {
        //1.3. Current closing (to be carried out anyway)
        try {
            if(w!=null){
                w.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 

 

 

 

 

Please give yourself a compliment!

Improve a little every day

​​​​​​​

Tags: Programming Mobile Java shell

Posted on Fri, 13 Mar 2020 22:21:33 -0400 by rawb