【JAVA】-【IO流】

Insert image description here
Insert image description here
Insert image description here
Insert image description here
You can also use byte streams to copy text files, but do not read them out in memory, such as system.out().
Insert image description here

Basic operations of FileReader reading data

The operation of IO stream can be summarized into 4 steps:

  1. Java is object-oriented. If you want to read a file, you must first have an object to represent the file, so:File file = new File("hello.text");
  2. Reading and writing files is a stream operation, so an instantiation of a stream is also required: FileReader fr = new FileReader(file);Here, because the text file is operated, the file content is read into the memory through characters, so the FileReader stream is used. After that, you To operate other content in other ways, you need to change the flow
  3. Perform operations on files: read in/write out
  4. Close the resource, that is, close the IO stream.
    Insert image description here
    The address of new File() in the main method is compared to this project, which is equivalent to that it is standing on this project, while the address of new File() in the test case is compared to
    Optimize this module
    Insert image description here
    : the code we wrote earlier handles exceptions by throwing exceptions, but if new FileReader(file) is executed to open the IO stream, and then fr.read() encounters an exception, the exception will be thrown out, resulting in fr.close() not being executed and the IO stream not being closed. There are resource waste and memory leak problems. Therefore, try...
    Insert image description here
    catch...finally should be used to catch exceptions to ensure that no matter where an exception occurs, IO Streams will be closed
    Insert image description here
    Insert image description here
    Insert image description here

Using reader() in FileReader

If the end of file is reached, -1 is returned. reader(char[] cbuf): It is equivalent to the reader using cbuf to load the content in the file. If the content in the file is greater than the length of cbuf, then cbuf characters can be read. Otherwise, the read file content is less than the length of cbuf. There will be unrecovered data in the cbuf array, which is the reason for the wrong writing. For example: If the content of the hello.txt file is: helloworld123, then the first time it is read is hello, the second time it is read is world, and the third time it is read is 123ld, so it is judged that each time it is read How many characters are there? Use the return value of reader(char[] cbuf) instead of the length of the cbuf array.
Insert image description here
Insert image description here

FileWrite operation to write out data

Insert image description here
Insert image description here
Insert image description here

Use FileInputStream and FileOutputStream to manipulate images

Insert image description here
Insert image description here

Buffered stream (byte type) implements copying of non-text files

Buffered stream: In order to improve the reading and writing efficiency of files, the reason why it can improve the reading and writing efficiency is that it provides an internal buffer area. When reading, it will first read the file into the buffer. When the buffer is full, Once again written out, the default size of the buffer is 8 bytes. There is a flush() method in bufferOutputStream for refreshing the buffer, that is, reading and writing data in the buffer and then clearing it.
Generally, buffered streams are used for development.
Please add image description
Please add image description
Closing of inner flow can be omitted
Please add image description

Guess you like

Origin blog.csdn.net/CaraYQ/article/details/132028415