当前位置:网站首页>File class learning

File class learning

2022-06-11 18:04:00 liaowenxiong

brief introduction

java.io.File Is an abstract representation of the path names of files and directories , Mainly used for the creation of files and directories 、 Search and delete operations

Constant

public static final char separatorChar //  This constant holds the system default file name separator , Save as character 
public static final String separator //  This constant holds the system default file name separator , Save as a string 
public static final char pathSeparatorChar //  This constant holds the system default path separator , Save as character 
public static final String pathSeparator //  This constant holds the system default path separator , Save as a string 

notes :Windows And the class Unix The file separator of the system is different , So you can't write dead in the project code , It is recommended to use File.separator() Replace .

Common methods

public String getAbsolutePath(); //  Access to this File The absolute pathname string for 
public String getPath(); //  Access to this File Full path name string of 
public String toString(); //  Access to this File Full path name string of ,toString() Default call getPath()
public String getName(); //  Access to this File Represents the name of a file or directory 
public long length(); //  Access to this File Represents the size of the file , In bytes , Directories have no concept of size , So you can't get the size of the directory 

public boolean exists(); //  this File Indicates whether the file or directory exists 
public boolean isDirectory(); //  this File Indicates whether the is a directory 
public boolean isFile(); //  this File Is it a document 

public boolean createNewFile(); //  Create this File File represented , When the file with the specified file name does not exist , Will create a new empty file , Otherwise... Will not be created 
public boolean delete(); //  Delete this File Represents a file or directory 
public boolean mkdir(); //  Create this File Table of contents , That is, create a single level directory 
public boolean mkdirs(); //  Create this File Table of contents , If the parent directory does not exist, it will be created together , Create a multi-level directory 

public String[] list(); //  Traverse this File Indicates the contents of the directory , Will not traverse the contents of subdirectories 
public File[] listFiles(); //  Traverse this File Indicates the contents of the directory , Will not traverse the contents of subdirectories  

Code example

View the system default file separator :

public static void main(String[] args) {
    
        String str = File.pathSeparator; 
        System.out.println(str); // MacOS The file separator for is a colon ":"
    }

Create an empty file :

public static void demo() {
    
        File file = new File("/Users/liaowenxiong/desktop/1.txt"); //  The file path name is specified in the constructor 
        try {
    
            boolean b = file.createNewFile(); //  If it is created successfully, it will return true
            System.out.println(b);
        } catch (IOException e) {
    
            e.printStackTrace();
        }
    }
原网站

版权声明
本文为[liaowenxiong]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203011853484978.html