Java基础-File


1. 什么是File

File:代表当前操作系统的文件,能过获取文件信息(大小,文件名,修改时间)、判断文件的类型、创建文件/文件夹、删除文件/文件夹
注意:File类只能对文件本身进行操作,不能读写文件里面存储的数据。

2 创建File类的对象

构造器说明
public File(String name)根据文件路径创建文件对象
public File(String parent, String child)根据父路径和子路径名字创建文件对象
public File(File parent, String child)根据父路径对应文件对象和子路径名字创建文件对象

注意:

  • File对象既可以是代表文件,也可以代表文件夹。
  • File封装的对象仅仅是一个路径名,这个路径可以是存在的,也允许是不存在的。
private static void Test1() {
//        File file = new File("D:\\repo\\javasepro\\helloworld-app\\src\\com\\ming\\File_\\ab.txt");
        File file = new File("helloworld-app/src/com/ming/File_/ab.txt");
        // 1. 获取文件大小【字节个数】
        System.out.println(file.length());
        // 2. 判断路径是否存在
        System.out.println(file.exists());
    }

3 File判断文件类型、获取文件信息功能

方法说明
public boolean exists()判断当前文件对象,对应的文件路径是否存在,存在返回True
public boolean isFile()判断当前文件对象指代的是否是文件,是文件返回True
public boolean isDirectory()判断是否是文件夹,是文件夹返回True
public String getName()获取文件名(包含后缀)
public long length获取文件大小,返回字节数
public long lastModified()获取文件的最后修改时间
public String getPath()获取创建文件对象时,使用的路径
public String getAbsolutePath()获取绝对路径
public boolean renameTo(File dest )修改文件、文件夹名称
private static void Test2() {
        // 1. 创建文件对象代指某个文件
        File file = new File("helloworld-app/src/com/ming/File_/ab.txt");
        // 2. public boolean exists()   判断当前文件对象,对应的文件路径是否存在,存在返回True
        System.out.println(file.exists());
        // 3. public boolean isFile()   判断当前文件对象指代的是否是文件,是文件返回True
        System.out.println(file.isFile());
        // 4. public boolean isDirectory()	 判断是否是文件夹,是文件夹返回True
        System.out.println(file.isDirectory());
        // 5. public String getName()   获取文件名(包含后缀)
        System.out.println(file.getName());
        // 6. public long length    获取文件大小,返回字节数
        System.out.println(file.length());
        // 7. public long lastModified()    获取文件的最后修改时间
        System.out.println(file.lastModified());

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = dateFormat.format(file.lastModified());
        System.out.println(format);
        // 8. public String getPath()	获取创建文件对象时,使用的路径
        System.out.println(file.getPath());
        // 9. public String getAbsolutePath()   获取绝对路径
        System.out.println(file.getAbsolutePath());
        // 10、public boolean renameTo(File dest )   修改文件名
        boolean b = file.renameTo(new File("helloworld-app/src/com/ming/File_/abc.txt"));
        System.out.println(b);
    }

4 File创建文件、删除文件

方法说明
public boolean createNewFile()创建一个新文件(文件内容为空),创建成功返回True
public boolean mkdir()创建文件夹,注意:只能创建一级文件夹
public boolean mkdirs()创建文件夹,注意:可以创建多级文件夹
public boolean delete()删除文件,或者空文件夹,注意:不能删除非空文件夹
private static void Test3() throws IOException {
        // 1. public boolean createNewFile()    创建一个新文件(文件内容为空),创建成功返回True
        File file = new File("helloworld-app/src/com/ming/File_/bdf.txt");
        file.createNewFile();
        // 2. public boolean mkdir()    创建文件夹,注意:只能创建一级文件夹
        File file2 = new File("helloworld-app/src/com/ming/File_/aaa");
        file2.mkdir();
        // 3. public boolean mkdirs()   创建文件夹,注意:可以创建多级文件夹
        File file3 = new File("helloworld-app/src/com/ming/File_/bbb/ccc/ddd");
        file3.mkdirs();
        // 4. public boolean delete()   删除文件,或者空文件夹,注意:不能删除非空文件夹
        File file4 = new File("helloworld-app/src/com/ming/File_/bbb");
        file3.delete();
    }

5 File遍历文件夹

方法说明
public String[] list()获取当前目录下所有的"一级文件名称"到一个字符串数组中
public File[] listFiles()(重点)获取当前目录下所有的"一级文件对象"到一个文件钟祥数组中
private static void Test4() {
        File file = new File("E:\\repo");
        // 1. public String[] list()    获取当前目录下所有的"一级文件名称"到一个字符串数组中
        String[] list = file.list();
        System.out.println(Arrays.toString(list));
        // 2. public File[] listFiles() (重点)获取当前目录下所有的"一级文件对象"到一个文件钟祥数组中
        File[] files = file.listFiles();
        for (File file1 : files) {
            System.out.println(file1.getPath());
        }
    }

在这里插入图片描述

  • 35
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Monly21

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

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

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

打赏作者

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

抵扣说明:

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

余额充值