IO stream knowledge sorting

IO stream

1 File class

1.1 construction method

  • public File(Sring pathName): create a new File instance by converting the given pathname string to an abstract pathname.
  • public File(String parent,String child): creates a new File instance from the parent pathname string and the child pathname string.
  • public File(File parent,String child): creates a new File instance from the parent abstract pathname and child pathname strings

Tips:

  1. A File object represents a File or directory that actually exists in the hard disk.
  2. Whether a File or directory exists in this path does not affect the creation of the File object

1.3 common methods

Methods of obtaining functions

  • public String getAbsolutePath(): returns the absolute path name string of this File.
  • public String getPath(): converts this File to a pathname string
  • public String getName(): returns the name of the File or directory represented by this File
  • public long length(): returns the length of the File represented by this File.
public class FileGet{
    public void main(String[] args){
        File f = new File("c:/aaa/bbb.java");
        System.out.println("File absolute path:" + f.getAbsolutePath());
        System.out.println("File construction path:" + f.getPath());
        System.out.println("File name:" + f.getName());
        System.out.println("file length" + f.length() + "byte");

        File f2 = new File("c:/aaa");
        System.out.println("Absolute path to directory:" + f2.getAbsolutePath());
        System.out.println("Construction path of directory:" + f2.getPath());
        System.out.println("Directory name" + f2.getName());
        System.out.println("Length of directory" + f2.length())
    }
}

length() indicates the length of the File, but if the File object indicates a directory, the return value is unspecified.

Absolute path and relative path

Absolute path: the path starting from the drive letter, which is a relatively complete path

Relative path: relative to the path of the project directory, this is a convenient path, which is often used in development

Note: Java must call the createNewFile() method to create a new File. Otherwise, if you use the File construction method alone to create a File, you cannot actually create a File (File f = new File("a.txt");

public class FileDemo01 {
    public static void main(String[] args) throws IOException {
        //a.txt file under Project
        File f = new File("a.txt");
        //Gets the construction path of the file
        System.out.println(f.getAbsolutePath());
        //Judge whether this File actually exists
        System.out.println(f.exists());
        //Create a new file with a relative path of "a.txt" under the project
        f.createNewFile();
        //Judge whether this File actually exists
        System.out.println(f.exists());
        //File deletion
        f.delete();
    }
}

Program code result:
C:\JavaSE\a.txt
false
true

Method of judging function

  • public boolean exists(): whether the File or directory represented by this File actually exists
  • public boolean isDirectory(): whether this File indicates a directory
  • public boolean isFile(): this File indicates whether it is a File
public class FileIs {
public static void main(String[] args) {
File f = new File("d:\\aaa\\bbb.java");
File f2 = new File("d:\\aaa");
// Judge whether it exists
System.out.println("d:\\aaa\\bbb.java Does it exist:" + f.exists());
System.out.println("d:\\aaa Does it exist:" + f2.exists());
// Determine whether it is a file or directory
System.out.println("d:\\aaa file?:" + f2.isFile());
System.out.println("d:\\aaa catalogue?:" + f2.isDirectory());
}
}
Output results:
d:\aaa\bbb.java Does it exist:true
d:\aaa Does it exist:true
d:\aaa file?:false
d:\aaa catalogue?:true

Method for creating and deleting functions

  • public boolean createNewFile(): creates a new empty file if and only if the file with this name does not exist yet
  • public boolean delete(): deletes the File or directory represented by this File.
  • public boolean mkdir(): create the directory represented by this File.
  • public boolean mkdirs(): create the directory represented by this File, including any required but nonexistent parent directory.

1.4 directory traversal

  • public Stirng[] list(): returns a String array representing all sub files or directories in the File directory.
  • public File[] listFiles(): returns a File array representing all sub files or directories in the File directory.

Note: the File object calling the listFiles method must represent the actual directory, otherwise it returns null and cannot be traversed

2-byte stream: the lowest unit

2.1 byte input stream: FileInputStream

read() read(byte[])
Return - 1 to read to the end of the file and end the cycle of reading the file

Character buffer stream: BufferedInputStream

Create character stream:

new FileInputStream(filename/file)

To create a buffered character stream:

new BufferedInputStream(new FileInputStream(filename/file))

2.2 byte output stream: FileOutputStream

write(int i) write(byte[])

When reading large files, you can read and write in an array of 1024 bytes to speed up the speed and improve the efficiency with buffered byte stream

Character output stream: BufferedOutputStream

Create character stream:

new FileOutputStream(filename/file,boolean)

To create a buffered character stream:

new BufferedInputStream(new FileInputStream(filename/file,boolean))

Note: if the boolean value added after the file name of the character output stream is true, it means that the file can be written additionally; The default value is false, which means that no additional writing is added to the file, and the contents of the original file are cleared for new writing.

2.3 function

It is mainly used for copying files; When the contents of the file output in the console do not need to be understood, byte stream is adopted.

Character stream is based on byte stream

3.1 input character stream InputStreamReader

read() read(char[])

Return - 1 to end the file

Create character stream

new InputStreamReader(new FileInputStream(file/filename),charset)

Note: adding charset later can be converted into your own character set to change the character set

3.2 output character stream OutputStreamReader

write() write(char[])

Create character stream

new OutputStreamReader(new FileOutputStream(file/filename),charset)

Note: adding charset later can be converted into your own character set to change the character set

byte -> character: decode
 character -> byte: code

Buffered character stream: Based on character stream

4.1 buffered character input stream BufferedReader

readLine() read per line
Returns a null value when reading to the end of the file

new BufferedReader(new InputStreamReader(new FileInputStream(file/filename)))

4.2 buffered character output stream PrintWriter

Print() println() (automatic refresh is valid for it)

new PrintWriter(new OutputStreamReader(new FileOutputStream(file/filename)),boolean  autoFlush)

Note: autoFlush is only valid for println(), and print() still needs to refresh flush() manually, otherwise there will be nothing in the output file

Note: the IO stream needs to close the output stream and resources

Serialization: Serializable

object -> Bytes: serialization -> Files: persistence
 byte -> Objects: deserialization
transient: Decorated property values are not serialized
serialVersionUID: Fixed the version number of the class
 If it is not fixed, it will be generated automatically and will change with the content of the class
public static final long  serialVersionUID = xxxxL;

Keywords: Java

Added by icm on Sat, 15 Jan 2022 00:18:51 +0200