File

代码1

/*
File:可以表示一个文件或者目录

* */
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.util.Date;

public class Demo8 {
    @Test
    public void test1() throws IOException {
        File file = new File("E://2,txt");
        boolean isExists=file.exists();
        System.out.println(isExists);//false

        //File.separator 盘符的分割方式
        File file2 = new File("E:"+File.separator+"2021072970"+File.separator+"12.png");
        System.out.println(file2);//E:\2021072970\12.png
        boolean isExists2=file2.exists();
        System.out.println(isExists2);//true

        File file3 = new File("E:\\2021072970\\12.png");
        long fileLength=file3.length();
        System.out.println(fileLength);//2788,单位是字节,Windows中右键文件属性即可查看

        String fileName = file3.getName();
        System.out.println(fileName);//12.png

        String filePath = file3.getPath();
        System.out.println(filePath);//E:\2021072970\12.png

        //时间
        long mTime = file3.lastModified();
        System.out.println(mTime);//1635061084521

        Date date1 = new Date(mTime);
        System.out.println(date1.toLocaleString());//2021-10-24 15:38:04

        //路径
        File file4 = new File("hello.java");
        System.out.println(file4.getPath());//hello.java 构建路径,相对路径
        System.out.println(file4.getAbsolutePath());//D:\idea\day07\optest\hello.java 绝对路径
        System.out.println(file4.getCanonicalPath());//D:\idea\day07\optest\hello.java 规范路径

        File file5 = new File("../../hello.java");
        System.out.println(file5.getPath());//..\..\hello.java 构建路径 相对路径
        System.out.println(file5.getAbsolutePath());//D:\idea\day07\optest\..\..\hello.java 绝对路径
        System.out.println(file5.getCanonicalPath());//D:\idea\hello.java 规范路径

        //创建新文件
        File file6 = new File("E:\\hello2.txt");
        boolean isNewFile = file6.createNewFile();//创建新文件
        System.out.println(isNewFile);//true

        //删除文件
        boolean isDelete = file6.delete();
        System.out.println(isDelete);//true

        //创建目录,删除空目录
        File file7 = new File("E:\\aa");
        file7.mkdir();
        file7.delete();

        //创建多级目录,删除目录
        File file8 = new File("E:\\bb\\cc\\dd");
        file8.mkdirs();
        file8.delete();


    }
}

代码2

 

import org.junit.Test;

import java.io.File;
import java.util.Date;

public class Demo9 {
    @Test
    public void test1(){
        File file1 = new File("E:\\12.txt");
        //设置文件不可写
        file1.setWritable(false);
        file1.setWritable(true);

        File file2 = new File("E:\\清史编年");
        //返回字符串数组
        String[] dirList=file2.list();
        for (String s:dirList){
            System.out.println(s);
        }
        /*
        01《清史编年+第一卷+(顺治朝)》.pdf
        02《清史编年+第二卷+(康熙朝)上》.pdf
        03《清史编年+第三卷+(康熙朝)下》.pdf
        04《清史编年+第四卷(雍正朝)》.pdf
        05《清史编年+第五卷(干隆朝)上》.pdf
        06《清史编年+第六卷(干隆朝)下》.pdf
        07《清史编年+第七卷(嘉庆朝)》.pdf
        08《清史编年+第八卷(道光朝)》.pdf
        09《清史编年+第九卷(咸丰朝)》.pdf
        10《清史编年+第十卷(同治朝)》.pdf
        11《清史编年+第十一卷(光绪朝)上》.pdf
        12《清史编年+第十二卷+(光绪朝下》.pdf
        */
        System.out.println("------------------------------");

        File file3 = new File("E:\\清史编年");
        //返回文件对象数组
        File[] fileList =file3.listFiles();
        for (File file : fileList) {
            System.out.println(file);
        }
        /*
        E:\清史编年\01《清史编年+第一卷+(顺治朝)》.pdf
        E:\清史编年\02《清史编年+第二卷+(康熙朝)上》.pdf
        E:\清史编年\03《清史编年+第三卷+(康熙朝)下》.pdf
        E:\清史编年\04《清史编年+第四卷(雍正朝)》.pdf
        E:\清史编年\05《清史编年+第五卷(干隆朝)上》.pdf
        E:\清史编年\06《清史编年+第六卷(干隆朝)下》.pdf
        E:\清史编年\07《清史编年+第七卷(嘉庆朝)》.pdf
        E:\清史编年\08《清史编年+第八卷(道光朝)》.pdf
        E:\清史编年\09《清史编年+第九卷(咸丰朝)》.pdf
        E:\清史编年\10《清史编年+第十卷(同治朝)》.pdf
        E:\清史编年\11《清史编年+第十一卷(光绪朝)上》.pdf
        E:\清史编年\12《清史编年+第十二卷+(光绪朝下》.pdf
        * */
        System.out.println("------------------------------");
        File file4 = new File("E:\\清史编年");
        File[] filesList=file4.listFiles();
        for (File file : filesList) {
            if (file.isFile()){
                System.out.println(file+" is file!");
            }else if (file.isDirectory()){
                System.out.println(file+" is Directory!");
            }
        }
        /*
        E:\清史编年\01《清史编年+第一卷+(顺治朝)》.pdf is file!
        E:\清史编年\02《清史编年+第二卷+(康熙朝)上》.pdf is file!
        E:\清史编年\03《清史编年+第三卷+(康熙朝)下》.pdf is file!
        E:\清史编年\04《清史编年+第四卷(雍正朝)》.pdf is file!
        E:\清史编年\05《清史编年+第五卷(干隆朝)上》.pdf is file!
        E:\清史编年\06《清史编年+第六卷(干隆朝)下》.pdf is file!
        E:\清史编年\07《清史编年+第七卷(嘉庆朝)》.pdf is file!
        E:\清史编年\08《清史编年+第八卷(道光朝)》.pdf is file!
        E:\清史编年\09《清史编年+第九卷(咸丰朝)》.pdf is file!
        E:\清史编年\10《清史编年+第十卷(同治朝)》.pdf is file!
        E:\清史编年\11《清史编年+第十一卷(光绪朝)上》.pdf is file!
        E:\清史编年\12《清史编年+第十二卷+(光绪朝下》.pdf is file!
        E:\清史编年\新建文件夹 is Directory!
        * */
    }

    @Test
    public void test2(){
        File file1 = new File("E:\\python15期");
        listFileName(file1);
    }

    public static void listFileName(File file){
       File[] filesList = file.listFiles();
        for (File file1 : filesList) {
            if (file1.isDirectory()){
                listFileName(file1);
            }else if (file1.isFile()){
                System.out.println(file1+" "+new Date(file1.lastModified()).toLocaleString());
            }
        }
    }
}

 

posted @ 2022-01-19 18:21  从此重新定义啦  阅读(75)  评论(0编辑  收藏  举报