The roommate's study materials may not be preserved!

Recently, Xiaolang's roommate has been immersed in learning materials, and every night is singing. As the father of the children, I naturally go online. Help me delete some. Generally, the suffixes of those learning materials are very consistent. We can use traversal to find all the videos. , all at once, so that the roommate will not be addicted. Warm reminder: don't try it casually, it is easy to be beaten.
insert image description here

IO.File type

basic type

变量和类型 字段 描述 
static String pathSeparator  与系统相关的路径分隔符,为方便起见,表示为字符串。  
static char pathSeparatorChar 与系统相关的路径分隔符。  
static String separator 系统相关的默认名称分隔符,为方便起见,表示为字符串。  
static char separatorChar 系统相关的默认名称分隔符。  

public static void main(String[] args) throws IOException {
    
    

        File file =new File("D://wa");
        //创建文件夹
        file.mkdir();
        //在文件夹里面添加文件
        File file1 =new File(file,"a.txt");
        file1.createNewFile();
        File file2 =new File("D://wa","b.txt");
        file2.createNewFile();
        //删除文件
        file1.delete();
        file2.delete();
        File file3 =new File("MyExpress.txt");
        //测试应用程序是否可以执行此抽象路径名表示的文件。
        boolean flag = file3.canExecute();
        //测试应用程序是否可以读取此抽象路径名表示的文件。
        boolean flag1= file3.canRead();
        //测试应用程序是否可以修改此抽象路径名表示的文件。
        boolean flag2= file3.canWrite();
        //测试此抽象路径名表示的文件或目录是否存在。
        boolean flag3=file3.exists();
        //返回此抽象路径名的绝对形式。
        file3.getAbsoluteFile();
        File file4 =new File("D:\\wa");
        File file5 =new File("D:\\wa.txt");
        file5.createNewFile();
        file5.renameTo(file4);
        System.out.println(flag);
        System.out.println(flag1);
        System.out.println(flag2);
        System.out.println(flag3);
    }

file traversal

public static void main(String[] args) throws IOException {
    
    
        File e =new File("d://");
        File [] files =e.listFiles();
        listFiles(files);
    }
    public static void listFiles(File[] files){
    
    
        if(files != null && files.length>0){
    
    
            for (File file:files){
    
    
                if(file.isFile()){
    
    
                    //文件
                    if(file.getName().endsWith("txt")){
    
    
                        if(file.length()>1024) {
    
    
                            System.out.println("找到txt文件" + file.getAbsolutePath());
                            //删除大于1kb的
                            file.delete();
                        }
                    }
                }else {
    
    
                    //文件夹
                    File[] file2 = file.listFiles();
                    listFiles(file2);
                }

            }
        }
    }

Relative and absolute paths

Absolute path: starting from the drive letter, is a complete path, such as: c://a.txt
Relative path: is an incomplete convenient path, such as: a.txt

public static void main(String[] args) throws IOException {
    
    
        File file =new File("D:a.txt");
        File file1 =new File("a.txt");
        System.out.println("file的路径:"+file.getAbsolutePath());
        System.out.println("file1的路径:"+file1.getAbsolutePath());
        //file的路径:D:\\a.txt
        //file1的路径:F:\XZk\a.txt
    }

insert image description here
Bullshit time:

If the guest officer thinks it is suitable to eat, can you give a free like! Thank you! Slow down guest officer! It is recommended to pack and collect, and come back next time. The shop's second QQ: 309021573, welcome to harass!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324212470&siteId=291194637
May