File class details (get the file name, size, path, create, etc.)

Overview

java.io.FileClasses are abstract representations of file and directory pathnames, and are mainly used for operations such as file and directory creation, search, and deletion.

Construction method

  • public File(String pathname): Creates 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 child pathname string .
  • public File(File parent, String child): Creates a new File instance from the parent abstract pathname and child pathname strings .
  • The construction example, the code is as follows:
// File path name
String pathname = "D:\\aaa.txt";
File file1 = new File(pathname); 

// File path name
String pathname2 = "D:\\aaa\\bbb.txt";
File file2 = new File(pathname2); 

// By parent path and child path string
 String parent = "d:\\aaa";
 String child = "bbb.txt";
 File file3 = new File(parent, child);

// By parent File object and child path string
File parentDir = new File("d:\\aaa");
String child = "bbb.txt";
File file4 = new File(parentDir, child);

💡Tips:

  1. A File object represents a file or directory that actually exists on the hard disk.
  2. Regardless of whether there is a file or directory in the path, it does not affect the creation of the File object.

Common method

How to get features

  • public String getAbsolutePath(): Returns the absolute pathname string of this File.
  • public String getPath(): Convert 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.
    Method demonstration, the code is as follows:
public class FileGet {
    public static void main(String[] args) {
        File f = new File("d:/aaa/bbb.java");     
        System.out.println("Absolute path to the file:"+f.getAbsolutePath());
        System.out.println("File construction path:"+f.getPath());
        System.out.println("File name: "+f.getName()); 
        System.out.println("File length: "+f.length()+"bytes");

        File f2 = new File("d:/aaa");     
        System.out.println("Absolute path to directory:"+f2.getAbsolutePath());
        System.out.println("Directory construction path:"+f2.getPath());
        System.out.println("Catalog Name:"+f2.getName());
        System.out.println("Directory length:"+f2.length());
    }
}

Output results.
File absolute path :d:\aaa\bbb. java
File construction path :d:\aaa\bbb. java
File name :bbb. java
File length :636 bytes

Directory absolute path :d:\aaa
Directory construction path :d:\aaa
Directory name :aaa
Directory length:4096

Description in the API: length(), which indicates the length of the file. But the File object represents a directory, the return value is unspecified.

Absolute and relative paths

  • Absolute path : The path starting from the drive letter, this is a complete path.
  • Relative path : The path relative to the project directory, this is a convenient path and is often used in development.
public class FilePath {
    public static void main(String[] args) {
      	//The bbb.java file under the D drive
        File f = new File("D:\\bbb.java");
        System.out.println(f.getAbsolutePath());
      	
		// The bbb.java file under the project
        File f2 = new File("bbb.java");
        System.out.println(f2.getAbsolutePath());
    }
}
Output results.
D:\bbb.java
D:\idea_project_test4\bbb.java

How to judge function

  • public boolean exists(): Whether the file or directory represented by this File actually exists.
  • public boolean isDirectory(): Whether this File represents a directory.
  • public boolean isFile(): Whether this File represents a file or not.

Method demonstration, the code is as follows:

public class FileIs {
    public static void main(String[] args) {
        File f = new File("d:\\aaa\\bbb.java");
        File f2 = new File("d:\\aaa");
        System.out.println("d:\\aaa\\bbb.java Does it exist:"+f.exists());
        System.out.println("d:\\aaa Does it exist:"+f2.exists());
      	// Determine if it is a file or a directory
        System.out.println("d:\\aaa file?:"+f2.isFile());
        System.out.println("d:\\aaa Catalog?:"+f2.isDirectory());
    }
}
Outcome:
d:\aaa\bbb.java Does it exist:true
d:\aaa Does it exist:true
d:\aaa file?:false
d:\aaa Catalog?:true

How to create a delete function

  • public boolean createNewFile(): Creates a new empty file if and only if a file with that name does not already 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 required but non-existing parent directories.

Method demonstration, the code is as follows:

public class FileCreateDelete {
    public static void main(String[] args) throws IOException {
        File f = new File("aaa.txt");
        System.out.println("Does it exist:"+f.exists()); // false
        System.out.println("Whether to create:"+f.createNewFile()); // true
        System.out.println("Does it exist:"+f.exists()); // true
		
      	File f2= new File("newDir");	
        System.out.println("Does it exist:"+f2.exists());// false
        System.out.println("Whether to create:"+f2.mkdir());	// true
        System.out.println("Does it exist:"+f2.exists());// true

      	File f3= new File("newDira\\newDirb");
        System.out.println(f3.mkdir());// false
        File f4= new File("newDira\\newDirb");
        System.out.println(f4.mkdirs());// true
      
       	System.out.println(f.delete());// true
      
        System.out.println(f2.delete());// true
        System.out.println(f4.delete());// false
    }
}

💡Description in API: delete method, if this File represents a directory, the directory must be empty to delete.

directory traversal

  • public String[] list(): Returns a String array representing all subfiles or directories in the File directory.
  • public File[] listFiles(): Returns a File array, representing all subfiles or directories in the File directory.
public class FileFor {
    public static void main(String[] args) {
        File dir = new File("d:\\java_code");
      
      	//Get the names of the files and folders in the current directory.
		String[] names = dir.list();
		for(String name : names){
			System.out.println(name);
		}
        //Get the file and folder objects in the current directory, as long as you get the file object, then you can get more information
        File[] files = dir.listFiles();
        for (File file : files) {
            System.out.println(file);
        }
    }
}

💡Tips:
The File object that calls the listFiles method must represent the actual directory, otherwise it returns null and cannot be traversed.

Read More:

Leave a Reply

Your email address will not be published. Required fields are marked *