File stream in Java

File input and output in Java

File class

The File class is the only data type in Java that represents file objects and directory objects

File object creation methods (four types)
1. File (File dir, String child) Create a new File instance based on the parent abstract path name and child path name string.
*** 2. File (String pathName) Create a new File instance by converting the given path name string to an abstract path name. (Most used)
3.File (String parent, String child) Create a new File instance based on the parent pathname string and the child pathname string
4.File (URI uri) By converting the given file: URI into a Abstract path name to create a new File instance

It should be noted that no matter whether the given file virtual path exists, the File object created must not be null in java, but the successful creation of the file object does not mean that this file must be stored on the hard disk

File stream classification

According to the direction, it can be divided into input stream (for reading files) and output stream (for writing files).
According to the content, it can be divided into byte streams (reading and writing binary files) and character streams (reading and writing) Text files
are divided according to the way, can be divided into node flow (for file creation) and processing flow (for other file flow creation)

Class inheritance relationship in byte input stream
Insert picture description here
Among them, BufferedInputStream buffer stream (processing stream) is based on the FileInputStream file stream for file input with a relatively large amount of data.
The main method of byte input stream It
Insert picture description here
should be noted here that the close () method must be executed after the end of the file stream, otherwise it will occupy system resources and have a certain impact on system performance.

Class inheritance in
Insert picture description here
character input streams The main methods of character input streams
Insert picture description here

Byte output stream class inheritance relationship
Insert picture description here
Similarly, BufferedOutputStream buffer stream (processing stream) is based on the FileOutputStream file stream for file output with a relatively large amount of data.
The main method in the
Insert picture description here
byte output stream The class inheritance relationship in the
Insert picture description here
character output stream The main method of the character output stream The
Insert picture description here
JAVA language internally converts the byte stream into a character stream. This conversion is done through InputStreamReader and OutputStreamReader As follows:
InputStreamReader (InputStream in, String charname): Use the specified encoding specification to convert the byte input stream to a character input stream, otherwise an exception is thrown.
OutputStreamReader (OutputStream out, String charname): Use the specified encoding specification to convert the byte output stream to a character output stream, otherwise an exception is thrown.

Java serialized read and write

Java not only provides the function of reading and writing files in the form of bytes and characters, but also provides the ability to read and write any Java data type.
The operation of storing a data in a file in the form of a Java object or obtaining a Java object from the target file is called a serialization operation, and the object that can be serialized is called a serialization object

What to pay attention to in the java file stream

1. Create or delete a file, folder and createNewFile (), delete (), mkdir (), mkdirs () function in Java.
2. Functions for judging files: uses of exists (), isFile (), isAbsolute (), isDirectory (), canRead (), canWrite (), isHidden () functions.
3. File attribute functions: use of lastModified (), length (), list (), listFiles (), renameTo (), getName (), getParent (), getPath (), getAbsolutePath (), delete () functions.
4. Use and differences of FileInputStream () and InputStreamReader () in file input and output operations.

Published 8 original articles · Likes2 · Visits 500

Guess you like

Origin blog.csdn.net/qq_42003546/article/details/100042947