I'm afraid the roommate's study materials will not be kept!

Recently, Xiaolang’s roommates are immersed in learning materials. Ye Ye Shengge. As the father of children, I naturally go online. Help delete some. Generally, the suffixes of those learning materials are very consistent. We can use traversal to find all the videos. , Sweep it all in one go, so that your roommate will not be addicted. Warm reminder: Don't try casually, it is easy to be beaten.
Insert picture 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 path and absolute path

Absolute path: starting from the drive letter, it is a complete path, for example: c://a.txt
Relative path: is an incomplete convenient path, for example: 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 picture description here
Nonsense time:

If the customer thinks that the food is appropriate, can you give a free like! Thank you! Slow walker officer! It is recommended to pack and collect and come again next time. Shop Xiaoer QQ: 309021573, welcome to harass!

Guess you like

Origin blog.csdn.net/AzirBoDa/article/details/113566792