File 类的基本用法 --《JAVA编程思想》76

File 可以代表文件或者目录,还可以查看文件的特性或者进行修改、删除操作,特此记录和大家一次分享。

1.文件是否存在

  • exists() 可判断文件是否存在
    public static void main(String[] args) throws IOException {
        File file = new File("123.txt");
        System.out.println("文件是否存在:" + file.exists());
    }
文件是否存在:true

2.创建文件

  • createNewFile(String pathName) 可创建空文件, pathName 为文件路径
    public static void main(String[] args) throws IOException {
        File file = new File("abc.txt");
        //判断文件是否存在
        if (!file.exists()) {
            System.out.println("文件是否存在:" + file.exists());
            //不存在则创建文件
            file.createNewFile();
        }
        System.out.println("文件是否存在:" + file.exists());
    }
文件是否存在:false
文件是否存在:true

3.创建文件夹

  • mkdir() 创建单级目录
  • mkdirs() 创建多级目录
    public static void main(String[] args) throws IOException {
        File firstDir = new File("firstDir");
        System.out.println("文件是否存在:" + firstDir.exists());
        firstDir.mkdir();
        System.out.println("文件是否存在:" + firstDir.exists());
        File secondDir = new File("firstDir/secondDir/file");
        System.out.println("文件是否存在:" + secondDir.exists());
        secondDir.mkdirs();
        System.out.println("文件是否存在:" + secondDir.exists());
    }
文件是否存在:false
文件是否存在:true
文件是否存在:false
文件是否存在:true

4.获取文件名和文件路径

  • getName() 获取文件名

  • getPath() 获取构造 File 类时传入的参数 pathName
    在这里插入图片描述

  • getAbsolutePath() 获取当前目录 + getPath() 返回的值

  • getCanonicalPath() 获取文件绝对路径

    public static void main(String[] args) throws IOException {
        File file = new File("./firstDir/secondDir/file/abc.txt");
        file.createNewFile();
        System.out.println(file.getName());
        System.out.println(file.getPath());
        System.out.println(file.getAbsolutePath());
        System.out.println(file.getCanonicalPath());
    }
abc.txt
.\firstDir\secondDir\file\abc.txt
E:\workSpace\thinkingInJava\.\firstDir\secondDir\file\abc.txt
E:\workSpace\thinkingInJava\firstDir\secondDir\file\abc.txt

5.文件重命名

  • renameTo(File dest) 可以将文件重命名,dest 为目标 File
    public static void main(String[] args) throws IOException {
        File oldFile = new File("abc.txt");
        oldFile.createNewFile();
        File newFile = new File("123.txt");
        System.out.println(newFile.exists());
        oldFile.renameTo(newFile);
        System.out.println(newFile.exists());
        System.out.println(oldFile.exists());
    }
false
true
false

7.删除文件

  • delete() 可用于删除文件或者目录
    public static void main(String[] args) throws IOException {
        File file = new File("abc.txt");
        file.createNewFile();
        System.out.println(file.exists());
        file.delete();
        System.out.println(file.exists());
    }
true
false

8.文件大小

  • length() 返回文件大小,返回单位为:字节
    public static void main(String[] args) throws IOException {
        File file = new File("E:/workSpace/spring.dataSoucrce.zip");
        System.out.println(file.length()/1024.0+"KB");
    }
53.7294921875KB

9.最后修改日期

  • lastModified() 获取文件的最后修改日期,返回值为毫秒数
    public static void main(String[] args) throws IOException {
        File file = new File("E:/workSpace/spring.dataSoucrce.zip");
        System.out.println(file.lastModified());
    }
1561263030656

10.判断目录

  • isDirectory() 判断文件是否为目录
    public static void main(String[] args) throws IOException {
        File file = new File("E:/workSpace/spring.dataSoucrce.zip");
        System.out.println(file.isDirectory());
        file = new File("E:/workSpace/");
        System.out.println(file.isDirectory());
    }
false
true

11.获取目录内文件列表

  • listFiles() 可将当前目录下的文件当作 File 数组返回
    public static void main(String[] args) throws IOException {
        File file = new File("E:/workSpace/");
        File[] files = file.listFiles();
        for (File f : files) {
            System.out.println(f.getName());
        }
    }
.idea
geektime-spring-family
geektime-vue-1-master
geektime-vue-1-master.zip
geektime-zero
git
HBN
leetcode
  • listFiles(FilenameFilter filter) 拥有重载方法,传入 FilenameFilter 的实现类,可对文件列表进行过滤;
    public static void main(String[] args) throws IOException {
        File file = new File("E:/workSpace/");
        File[] javas = file.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                if (name.indexOf("zip") > -1) return true;
                return false;
            }
        });
        for (File java : javas) {
            System.out.println(java);
        }
    }
E:\workSpace\geektime-vue-1-master.zip
E:\workSpace\mybatis-plus-demo.zip
E:\workSpace\spring.dataSoucrce.zip

12.判断文件可否读写

  • canRead() 判断文件是否可读
  • canWrite() 判断文件是否可写
    public static void main(String[] args) throws IOException {
        File file = new File("E:/workSpace/");
        System.out.println(file.canRead());
        System.out.println(file.canWrite());
    }
true
true

本次分享至此结束,希望本文对你有所帮助,若能点亮下方的点赞按钮,在下感激不尽,谢谢您的【精神支持】。

若有任何疑问,也欢迎与我交流,若存在不足之处,也欢迎各位指正!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BaymaxCS

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值