File Class Common Methods

Keywords: Java

File refers to the abstract representation of file and directory path names.

Construction method:

File(String pathname): Get a File object from a path

File(String parent, String child): Get File objects from a directory and a subfile/directory

File(File parent, String child): Get a File object from a parent File object and a child file/directory

//File(String pathname)
File file1 = new File("a.txt");
//File(String parent, String child):
File file2 = new File("D:\\Java\\test","a.txt");
//File(File parent, String child):
File parent = new File("D:\\Java\\test");
File file3 = new File(parent,"a.txt");

Common member methods:

public boolean createNewFile(): Create a file If such a file exists, it will not be created.Creation returns true successfully and failure returns false

public boolean mkdir(): Create a folder If such a folder exists, it will not be created

public boolean mkdirs(): Create folders for you, if the parent folder does not exist

public boolean renameTo(File dest): Rename the file to the specified file path.If the source file and the target file are in the same directory, the result is to rename the source file to the target file; otherwise, cut the source file to the target file directory and rename it

public boolean delete(): Delete files or folders

public boolean isDirectory(): Determine if it is a directory?

public boolean isFile(): Determine if it is a file?

public boolean exists(): Determine if it exists

public boolean canRead(): Determine if it is readable

public boolean canWrite(): Determine Writability

public boolean isHidden(): Determine whether to hide

public String getAbsolutePath(): Get absolute path, starting with drive letter

public String getPath(): Get Path

public String getName(): Get the name

public long length(): the number of bytes to get the contents of the file

public long lastModified(): Gets the last modification time, in milliseconds

public String[] list(): Gets an array of names of all files or folders in the specified directory

public File[] listFiles(): Gets an array of Files for all files or folders in the specified directory

 

FilenameFilter: A file name filter used to filter file names.Usually used in the list() and listFiles() methods of the File class

The source code is as follows:

@FunctionalInterface
public interface FilenameFilter {
    /**
     * Tests if a specified file should be included in a file list.
     *
     * @param   dir    the directory in which the file was found.
     * @param   name   the name of the file.
     * @return  <code>true</code> if and only if the name should be
     * included in the file list; <code>false</code> otherwise.
     */
    boolean accept(File dir, String name);

 

Case: Get the filename with the suffix.txt under the specified folder

package myfile;

import java.io.File;
import java.io.FilenameFilter;

public class FileTest2 {
    public static void main(String[] args) {
        File file = new File("D:\\Java\\test\\");
        String[] fileNames = file.list(new MyFilenameFilter());
        for(String filename: fileNames){
            System.out.println(filename);
        }
    }
}

class MyFilenameFilter implements FilenameFilter {
    /**
     * 
     * @param dir   Directory Name
     * @param name  File name under directory
     * @return  Returns true when name meets specified requirements, false otherwise
     */
    @Override
    public boolean accept(File dir, String name) {
        if(name.endsWith("txt")){
            return true;
        }
        return false;
    }
}

Posted by SmokyBarnable on Mon, 29 Apr 2019 03:00:38 -0700