Overview of File class of Java IO stream

Insert picture description here

File

System and path separators

  • Separator related to system and path

Insert picture description here

public class Demo01FIle {

    public static void main(String[] args) {

        String pathSeparator = File.pathSeparator;
        System.out.println(pathSeparator);  // 结果是分号  代表是windows系统

        String separator = File.separator;
        System.out.println(separator);      // 结果是反斜杠\  代表文件名称分隔符

    }

}

Absolute path and relative path

  • Absolute path: is a completed path starting with a drive letter

C:\a.txt
C:\Users\itcast\IdeaProjects\a.txt
D:\demo\b.txt

  • Relative path: is a simplified path

Relatively refers to the root directory relative to the current project (C: \ Users \ itcast \ IdeaProjects \ a.txt)
If relative to the root directory of the current project, the path can be simplified writing
C: \ Users \ itcast \ IdeaProjects \ demo \ 123 .txt-> simplified to 123.txt (can be omitted as root directory)

note:

  • The path is not case sensitive
  • The file name separator in the path uses backslashes for windows. Backslashes are escape characters. Writing 2 backslashes represents 1 backslash
  • The following figure is the constructor that initializes the FIle object
    Insert picture description here

File (String pathname)
String pathname: String pathname The
path can end with a file or a folder. The
path can be a relative path or an absolute path. The
path can exist or it doesn't exist.
Create a File object , Just encapsulate the string path as a FIle object, regardless of the true or false path

      File file = new File("G:\\1.jpg");
      System.out.println(file);   // 打印出来的就是  G:\1.jpg  
	
	  File file2 = new File("G:\\word\\office2003");
      System.out.println(file2);  // 打印 G:\word\office2003

File (String parent, String child)
String parent: parent path
String child: child path
Parent path and child path, can be written separately, flexible

	File file = new File("C:\\","1.jpg");
	System.out.println(file);  // 打印出来  C:\1.jpg;

File (File parent, String child)
File parent: parent path
String child: child path The
parent path and the child path can be written separately, the
parent path is flexible and variable . The file path can be used to perform some operations on the path using the FIle method, and then use the path Create Object

	File file = new File("C:\\");
	File file2 = new File(file,"1.jpg");
	System.out.println(file2); // 打印 C:\\1.jpg

Commonly used methods

Get method

  • public String getAbsolutePath () returns the absolute path name string of this File
  • public String getPath () Convert this File to a path name 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 static void main(String[] args) {
    File file2 = new File("G:\\word\\office2003\\office2003.rar");
    System.out.println(file2);
    String absolutePath = file2.getAbsolutePath();
    System.out.println("文件绝对路径:"+absolutePath);
    String path = file2.getPath();
    System.out.println("文件构造路径:"+path);
    String name = file2.getName();
    System.out.println("文件的名字:"+name);
    long length = file2.length();
    System.out.println("文件长度:"+length+"的字节");  
}

Insert picture description here

note:

  • public long length (): returns the length of the file represented by this File
  • The folder has no concept of size, and the folder size cannot be obtained
  • If the path given in the constructor does not exist, then the length method returns 0

Judgment method

  • public boolean exists () returns whether the path of this File exists
  • public boolean isDirectory () This File indicates whether it is a directory
  • public boolean isFile () This FIle indicates whether it is a file
  • public boolean canExecute () Whether this File can execute the file with path name
  • public boolean canRead () Whether this File is readable
  • public boolean canWrite () Whether this File is writable
public static void main(String[] args) {
     File file2 = new File("G:\\word\\office2003\\office2003.rar");
     boolean exists = file2.exists();
     System.out.println(exists);
     boolean canRead = file2.canRead();
     System.out.println(canRead);
     boolean canWrite = file2.canWrite();
     System.out.println(canWrite);
     boolean canExecute = file2.canExecute();
     System.out.println(canExecute);
     boolean directory = file2.isDirectory();
     System.out.println(directory);
     boolean file = file2.isFile();
     System.out.println(file);
}

Insert picture description here

How to delete

  • public boolean createNewFile () Currently only creates a new empty file if the file with this name does not yet exist
  • public boolean delete () deletes the file or directory represented by this File
  • public boolean mkdir () creates the directory represented by this File
  • public boolean mkdirs () creates the directory represented by this FIle, including any necessary but non-existing parent directories

note:

If the createNewFile () path does not exist, an IOExeception exception will be thrown.
If the delete () file / folder is successfully deleted, it returns true. If there is content in the folder, it will not be deleted and false will be returned. If the path does not exist in the constructor, false will be returned.

Traverse folder

  • public String [] list () returns a String array representing all sub-files or directories in the File directory
  • puublic File [] listFiles () returns a File array representing all sub-files or directories in the FIle directory
   File file = new File("G:\\");
   String[] list = file.list();
   for (String s : list) {
       System.out.println(s + ",");
   }

Insert picture description here

   File[] files = file.listFiles();
   for (File file1 : files) {
       System.out.println(file1.getPath() + "\n");
   }

Insert picture description here

Both are all files or folders under the printing path (just under the current path, excluding sub-paths), and all hidden files

Published 24 original articles · praised 33 · visits 2391

Guess you like

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