Java IO character stream overview-2

Insert picture description here

Character stream

When using a byte stream to read text, especially when encountering Chinese characters, the complete character may not be displayed. That is because a Chinese character may occupy multiple bytes of storage.
Java provides some character stream classes. Unit reads and writes data, specializing in text files

Character input stream [Reader]

The java.io.Reader abstract class represents the superclass of all classes used to read character streams, which can read character information into memory, defining the basic common methods of character input streams

Insert picture description here

File character input stream [FileReader]

Read the data in the hard disk file into the memory in character mode

The following picture is its constructor
Insert picture description here
! ! ! How to use! ! !

public class Demo01Reader {

    public static void main(String[] args) throws IOException {
        Reader reader = new FileReader("F:\\cccc.txt");
        // 存储读取1024个字符
        char[] chars = new char[1024];
        int len = 0;
        // 当读到文件结尾时候  len==-1
        while ((len = reader.read(chars)) != -1) {
            System.out.println(new String(chars, 0, len));
        }
        reader.close();
    }
}

! ! ! The result is correct! ! !

Insert picture description here

Character output stream [Writer]

The java.io.Writer abstract class represents the superclass of all classes used to write out character streams, and can read character information into memory, defining the basic common methods of character input streams

Insert picture description here

File character output stream [FileWriter]

The java.io.FileWriter class is a convenient class for writing out characters to a file, using the system's default character encoding and default byte buffer during construction

The following picture is its constructor

Insert picture description here
Character output stream use points:

  • Create a FileWriter object, bind the destination to write data in the constructor
  • Use the method writer in FileWriter to write data to the memory buffer (the process of converting characters to bytes)
  • Use the method flush in FileWriter to flush the data in the memory buffer to the file
  • Release resources (before this, the data in the memory buffer will be refreshed to the file)

How to use

public class Demo02Writer {

    public static void main(String[] args) throws IOException {

        Writer writer = new FileWriter("F:\\cccc.txt");
        writer.write("囧囧是世界上最漂亮的女孩");
        writer.close();

    }
}

Write successfully, and replace the contents of the previous file

Insert picture description here

Closed and refreshed

Because of the built-in buffer, if you do not close the output stream, you cannot write characters to the file, but the closed stream object cannot continue to write data.
If we want to write data, and another technology uses streams, we need flush method

  • flush: flush the buffer, the stream object can continue to use
  • close: refresh the buffer first, then notify the system to release resources, the stream object can no longer be used
public class Demo02Writer {

    public static void main(String[] args) throws IOException {

        Writer writer = new FileWriter("F:\\cccc.txt");
        writer.write("囧囧是世界上最漂亮的女孩");
        writer.flush();
        writer.write("囧囧是世界上最漂亮的女孩-2");
        writer.close();

    }
}

Insert picture description here

Note: The first call to the flush () method will force the buffer data to be written to the file. After calling close, you cannot continue to write data! ! !

Continue writing and wrapping

Similar to FileOutputStream, there is a boolean constructor
true for appending false for not appending
Insert picture description here
! ! ! Code on! ! !

public class Demo02Writer {

    public static void main(String[] args) throws IOException {

        Writer writer = new FileWriter("F:\\cccc.txt", true);
        writer.write("囧囧是世界上最漂亮的女孩" + "\r\n");
        writer.flush();
        writer.write("————囧囧是世界上最豆的女孩!" + "\r\n");
        writer.close();

    }
}

! ! ! The result is appended! ! !

Insert picture description here

jdk 9 vs. jdk 7

We know that the writing method of jdk7 can be used try ... with ... resource, it will not be covered here. It is very simple.
Next we look at the new writing method of jdk 9.

! ! ! The code above is still an example of copying files! ! !

public class Demo05FileInputStream {

    public static void main(String[] args) throws FileNotFoundException {
        FileInputStream fis = new FileInputStream("F:\\cccc.txt");
        FileOutputStream fos = new FileOutputStream("F:\\dddd.txt");

		// jdk 9 的新语法,支持将流放入圆括号中,省略了finally关闭流的操作
        try (fis; fos) {
            int len = 0;
            byte[] bytes = new byte[1024];
            while ((len = fis.read(bytes)) != -1) {
                fos.write(bytes, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("复制成功");

    }
}
Published 24 original articles · praised 33 · visits 2391

Guess you like

Origin blog.csdn.net/weixin_41241629/article/details/104336002