JAVA基础复习一 File类的常用功能

File类常用功能

  1. 当hello.txt文件实际并不存在时。
/**
     * File类的常用方法
     */
    @Test
    public void test2() {
        File file1 = new File("hello.txt");
        File file2 = new File("D:\\io\\hello.txt");
        //获取绝对路径
        String absolutePath = file1.getAbsolutePath();
        //获取当前的路径
        String path = file1.getPath();
        //获取文件或目录名称
        String name = file1.getName();
        //获取上一级的路径
        File parentFile = file1.getParentFile();
        //获取文件的长度
        long length = file1.length();
        //获取文件的上一次修改的时间戳
        long lastModified = file1.lastModified();
        System.out.println(absolutePath);
        System.out.println(path);
        System.out.println(name);
        System.out.println(parentFile);
        System.out.println(length);
        System.out.println(lastModified);
        System.out.println(new Date(lastModified));

        System.out.println();
        
        String absolutePath2 = file2.getAbsolutePath();
        String path2 = file2.getPath();
        String name2 = file2.getName();
        File parentFile2 = file2.getParentFile();
        long length2 = file2.length();
        long lastModified2 = file2.lastModified();
        System.out.println(absolutePath2);
        System.out.println(path2);
        System.out.println(name2);
        System.out.println(parentFile2);
        System.out.println(length2);
        System.out.println(lastModified2);

    }
  • 运行结果
D:\workspace_idea\basictrain\arrayTest\hello.txt
hello.txt
hello.txt
null
0
0
Thu Jan 01 08:00:00 CST 1970

D:\io\hello.txt
D:\io\hello.txt
hello.txt
D:\io
0
0
  1. 当hello.txt文件实际存在时。
  • 运行结果
D:\workspace_idea\basictrain\arrayTest\hello.txt
hello.txt
hello.txt
null
10
1593934629555
Sun Jul 05 15:37:09 CST 2020

D:\io\hello.txt
D:\io\hello.txt
hello.txt
D:\io
8
1593934696659
@Test
    public void test3() {
        File file1 = new File("D:\\workspace_idea\\basictrain\\arrayTest");
        //获取指定目录下的所有文件或目录名称
        String[] list = file1.list();
        //获取指定目录下的所有文件或目录的绝对路径
        File[] files = file1.listFiles();
        for (String s : list) {
            System.out.println(s);
        }
        System.out.println("-----------------------------------");
        for (File file : files) {
            System.out.println(file);
        }
    }
  • 运行结果
ArrayTest.iml
hello.txt
src
-----------------------------------
D:\workspace_idea\basictrain\arrayTest\ArrayTest.iml
D:\workspace_idea\basictrain\arrayTest\hello.txt
D:\workspace_idea\basictrain\arrayTest\src
    /**
     * 将指定文件重命名为另一个文件,注:操作成功后源文件会删除
     */
    @Test
    public void test4() {
        File file = new File("hello.txt");
        File file1 = new File("D:\\io\\hi.txt");
        boolean renameTo = file.renameTo(file1);
        System.out.println(renameTo);
    }
  • 注:上述实例要想返回结果为true,需要满足file的路径中存在hello.txt文件,file1路径中,不存在hi.txt文件才可以。(此方法很特殊)
    @Test
    public void test5() {
        File file = new File("hello.txt");
        //判断是否为一个目录
        System.out.println(file.isDirectory());
        //判断是否为一个文件
        System.out.println(file.isFile());
        //判断是否存在
        System.out.println(file.exists());
        //判断是否可读
        System.out.println(file.canRead());
        //判断是否可写
        System.out.println(file.canWrite());
        //判断是否隐藏
        System.out.println(file.isHidden());
    }
    @Test
    public void test6() throws IOException {
        File file = new File("hi.txt");
        if (!file.exists()) {
            //创建文件
            boolean newFile = file.createNewFile();
            System.out.println("文件创建成功");
        } else {
            //删除文件
            file.delete();
            System.out.println("文件删除成功");
        }
    }
    @Test
    public void test7() throws IOException {
        File file = new File("D:\\io\\io1\\i02");
        //最后一级目录的父级不存在,则不会创建最后一级目录
        boolean mkdir = file.mkdir();
        //最后一级目录的父级不存在,也会创建最后一级目录
        boolean mkdirs = file.mkdirs();
    }
posted @ 2020-07-05 15:40  温森  阅读(351)  评论(0编辑  收藏  举报