Use of the File class - Java


Usage of File

File is used to operate on files. The IO stream operates on the reading and writing of files, and does not have the ability to add or delete files.

Three ways to create a File

1. Directly specify the file path. (This refers to absolute paths.)

    private static void demo01() {
    
    
//        通过给定的路径名确定文件
        String path = "E:\\itheima\\a.txt";
//        使用File类中的方法
        File file = new File(path);

        System.out.println(file.getName());
        System.out.println(file.isFile());
    }

2. Use the parent path and filename.

private static void demo02() throws IOException {
    
    
    File f = new File("a.txt");
    if (!f.exists()) {
    
    
        f.createNewFile();
    }
    String path1 = "E:\\itheima";
    String path2 = "a.txt";
    File file = new File(path1, path2);
    System.out.println(file.getAbsolutePath());
}

3. Use the parent file object and file name.

private static void demo03() {
    
    
    File file = new File("E:\\itheima");
    String name = "a.txt";
    File file2 = new File(file,name);
    System.out.println(file2.getAbsolutePath());
}

How to create files

create empty file

createNewFile()

public boolean createNewFile();
创建一个新的空的文件。

create folder

f.mkdir(); //创建一个单级文件夹
f.mkdirs(); //创建一个多级文件夹。

Delete Files

File file = new File("E:\\itheima\\a.txt");
file.delete();
// public boolean delete(); 删除文件。

File class judgment and acquisition function

public boolean isDirectory() //测试此抽象路径名表示的file是否为目录
public boolean isFile() //测试该File是否为文件
public boolean exists() //测试该file是否存在
public String getName() //返回该路径名表示的文件或目录的名称。

File class advanced acquisition

listfiles();
File[] files = file.listFiles();

注意事项:
当调用者不存在时,返回null
当调用者是一个文件时,返回null
当调用者是一个空文件夹时,返回一个长度为0的数组。
当调用者是一个有内容的文件夹时,将里面所有文件和文件夹的路径放在File数组中返回。
当调用者是一个有隐藏文件的文件夹时,将里面所有文件和文件夹的路径放在File数组中返回,包含隐藏文件。
当调用者是一个需要权限才能进入的文件夹时,返回null
Exercise 1. Create a.txt file under the current folder aaa folder
private static void demo07() throws IOException {
    
    
    File file1 = new File("aaa");
    file1.mkdir();
    File file2 = new File(file1,"a.txt");
    boolean flag = file2.createNewFile();
    System.out.println(flag);
}
Exercise 2. Delete multi-level folders
    private static void deleteDir(File src) {
    
    
//        从该源文件中找到所有的子文件,
        //        首先进入该源文件夹,然后从该文件夹中查看所有的子文件,属于文件的直接删除,属于文件夹的递归,然后是文件的再删除。
//        最后删除src源文件。
        File[] files = src.listFiles();
        for (File file : files) {
    
    
            if (file.isFile()) {
    
    
                file.delete();
            } else if (file.isDirectory()) {
    
    
                deleteDir(file);
            }
        }
        src.delete();
    }
Exercise 3, count the number of files of various types in a source folder.
 private static void demo09() {
    
    
        File file = new File("E:\\BiliDown");
        HashMap<String, Integer> map = new HashMap<>();
        getCount(map, file);
        System.out.println(map);
    }

    //    计算各个类型的文件的数量。
    private static void getCount(HashMap<String, Integer> map, File file) {
    
    
//        首先进入该文件,获取所有子文件。
        File[] files = file.listFiles();
        for (File f : files) {
    
    
            if (f.isFile()) {
    
    
//                如果是文件的话
                String filename = f.getName();
                String[] filename_arr = filename.split("\\.");
                int length = filename_arr.length;
                String last_name = filename_arr[length - 1];
                boolean flag = map.containsKey(last_name);
                if (flag == true) {
    
    
//                        如果该后缀文件已经存在。
                    Integer number = map.get(last_name);
                    number++;
                    map.put(last_name, number);
                } else if (flag == false) {
    
    
//                        如果没有存在
                    map.put(last_name, 1);
                }
            } else if (f.isDirectory()) {
    
    
//                如果是文件夹的话,继续递归进入
                getCount(map, f);
            }
        }
    }

Guess you like

Origin blog.csdn.net/qq_45022687/article/details/122592112