欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

File类

程序员文章站 2022-06-16 21:22:28
...

import org.junit.Test;

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

/**
 * @author xianyu
 * @time 2019-08-16-19:16
 */
public class demo1 {

    @Test
    public void test(){

        //File类

        //构造器1
        File file1 = new File("hello.txt");  // 相对路径
        File file2 = new File("E:\\FileTest\\hi.txt");  // 绝对路径


        System.out.println(file1.getAbsolutePath());   //获取绝对路径  E:\ThirdApp\day02\hello.txt
        System.out.println(file1.getPath());           //获取路径      hello.txt
        System.out.println(file1.getName());           //获取名字      hello.txt
        System.out.println(file1.getParent());         //获取上层路径   null
        System.out.println(file1.length());            //获取文件大小   11个字节
        System.out.println(new Date(file1.lastModified()));      //获取文件最后一次修改时间   Sat Aug 17 21:24:19 CST 2019


        System.out.println("*****************************");

        System.out.println(file2.getAbsolutePath());   //获取绝对路径        E:\FileTest\hi.txt
        System.out.println(file2.getPath());           //获取路径            E:\FileTest\hi.txt
        System.out.println(file2.getName());           //获取名字            hi.txt
        System.out.println(file2.getParent());         //获取上层路径         E:\FileTest
        System.out.println(file2.length());            //获取文件长度         10个字节
        System.out.println(new Date(file2.lastModified()));      //获取文件最后一次修改时间    Sat Aug 17 21:27:06 CST 2019

        System.out.println("*****************************");

        //也可以获取目录
        //构造器2
        File file3 = new File("E:\\FileTest","Demo");


        //构造器3
        File file4 = new File(file3,"good.txt");
        System.out.println(file4.getAbsolutePath());   //获取绝对路径        E:\FileTest\Demo\good.txt
        System.out.println(file4.getPath());           //获取路径            E:\FileTest\Demo\good.txt
        System.out.println(file4.getName());           //获取名字            good.txt
        System.out.println(file4.getParent());         //获取上层路径        E:\FileTest\Demo
        System.out.println(file4.length());            //获取文件长度        13个字节
        System.out.println(new Date(file4.lastModified()));      //获取文件最后一次修改时间    Sat Aug 17 21:32:15 CST 2019


        System.out.println("*****************************");
        File file5 = new File("E:\\halo");

        String[] nameList = file5.list();     //获取指定目录下所有文件或文件目录的名称String数组

        for(String s : nameList){
            System.out.println(s);
        }

        System.out.println("*****************************");

        File[] nameList2 = file5.listFiles();  //获取指定目录下所有文件或文件目录的名称File数组
        for(File s : nameList2){
            System.out.println(s);
        }

        System.out.println("*****************************");

    }


    @Test
    public void test2(){

        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 test3() throws IOException {

        File file = new File("hello1.txt");
        if(!(file.exists())){
            //如果文件不存在就创建文件
            file.createNewFile();
            System.out.println("创建成功!!!!");
        }else{
            file.delete();
            System.out.println("删除成功");
        }
    }

    @Test
    public void test7(){
        //文件目录的创建
        File file1 = new File("e:\\io\\io1\\io3");

        boolean mkdir = file1.mkdir();
        if(mkdir){
            System.out.println("创建成功1");
        }

        File file2 = new File("e:\\io\\io1\\io4");

        boolean mkdir1 = file2.mkdirs();
        if(mkdir1){
            System.out.println("创建成功2");
        }
        //要想删除成功,io4文件目录下不能有子目录或文件
//        File file3 = new File("e:\\io\\io1\\io4");
        File file3 = new File("e:\\io\\io1");
        System.out.println(file3.delete());
    }



}

E:\ThirdApp\day02\hello.txt
hello.txt
hello.txt
null
11
Sat Aug 17 21:24:19 CST 2019
*****************************
E:\FileTest\hi.txt
E:\FileTest\hi.txt
hi.txt
E:\FileTest
10
Sat Aug 17 21:27:06 CST 2019
*****************************
E:\FileTest\Demo\good.txt
E:\FileTest\Demo\good.txt
good.txt
E:\FileTest\Demo
13
Sat Aug 17 21:32:15 CST 2019
*****************************
.gitattributes
.github
.gitignore
.gradle
.idea
.travis.yml
build.gradle
Dockerfile
gradle
gradlew
gradlew.bat
LICENSE
lombok.config
out
README.md
scripts
settings.gradle
src
*****************************
E:\halo\.gitattributes
E:\halo\.github
E:\halo\.gitignore
E:\halo\.gradle
E:\halo\.idea
E:\halo\.travis.yml
E:\halo\build.gradle
E:\halo\Dockerfile
E:\halo\gradle
E:\halo\gradlew
E:\halo\gradlew.bat
E:\halo\LICENSE
E:\halo\lombok.config
E:\halo\out
E:\halo\README.md
E:\halo\scripts
E:\halo\settings.gradle
E:\halo\src
*****************************
相关标签: File类