File类的理解和使用

399 篇文章 12 订阅
package com.example.demo;

import org.junit.Test;

import java.io.File;
import java.io.IOException;

/**
 * @Description File类的理解和使用
 * @Auther gf.x
 * @Date 2020/4/2 18:36
 */
public class FileTest {
    @Test
    public void test() throws IOException {

        //一、构造方法:
        //1. 创建文件:
        // ①、此时file对象就代表 E:/test/child/ 目录下的 TestFile.java文件。
        // ②、我们就可以通过操作file对象,来操作这个目录下的TestFile.java文件。
        File file = new File("E:/test/child/TestFile.java");

        //2. 创建目录:
        // ①、此时filePath对象就代表 E:/test/child 目录
        // ②、我们可以通过操作 filePath对象,来操作 E:/test/child 目录
        File path = new File("E:/test/child");

        //3. 创建文件:
        //通过目录、文件名创建文件对象
        File pathFile = new File(path, "TestFile1.java");

        //二、其它方法:
        //isFile():判断某个对象是不是一个文件
        if (file.isFile()) {
            System.out.println("file是一个文件");
        }

        //isDirectory():判断某个对象是不是一个目录
        if (path.isDirectory()) {
            System.out.println("path是一个目录");
        }

        //1. 创建新的文件/目录:
        //public boolean createNewFile():创建文件
        //public boolean mkdir():创建文件夹;如果父级路径不存在,则文件夹创建失败
        //public boolean mkdirs():创建文件夹;如果父级路径不存在,则自动创建父级路径,再创建子级路径
        File file5 = new File("E:/test1/child2");
        boolean mkdir2 = file5.mkdir();
        System.out.println(mkdir2); //false
        boolean mkdir3 = file5.mkdirs();
        System.out.println(mkdir3); //true

        File file4 = new File("E:/test/child1");
        File file2 = new File("E:/test/child/TestFile2.java");
        File file3 = new File("E:/TestFile3.java");
        File file6 = new File("E:/test1/child1/TestFile3.java");
        boolean newFile2 = file6.createNewFile(); //此处会报错:路径不存在
        boolean mkdir = file4.mkdir();
        System.out.println(mkdir); //true

        /**
         * 因为createNewFile()方法声明时抛出了异常,方法里面也抛出了异常;
         * 所以在调用这个方法的时候:
         *  ①要么也抛出异常。
         *  ②要么捕获异常。
         *
         * public boolean createNewFile() throws IOException {
         *      if (isInvalid()) {
         *          throw new IOException("Invalid file path");
         *      }
         * }
         */
        boolean newFile1 = file3.createNewFile();
        System.out.println(newFile1); //true
        boolean newFile = file2.createNewFile(); //true
        System.out.println(newFile);


        //2. 判断 文件/路径 属性:
        //public boolean canRead():判断 文件 是否可读
        //public boolean canWrite():判断 文件 是否可写
        //public boolean exists():判断 文件 是否存在
        //public boolean isAbsolute():判断 路径 是否为绝对路径
        //public boolean isHidden():判断 文件 是否为隐藏文件
        //final boolean isInvalid():判断 文件路径 是否有效

        //3. 获取 文件/路径 属性:
        //public long lastModified():获取 文件 最后修改时间
        //public long length():获取 文件 的长度
        //public String getName():获取 文件名字(路径文件字符串中的最后一个文件名)
        //public String getPath():获取 路径(完整路径)
        //public File getAbsoluteFile():获取 绝对路径文件
        //public String getParent():获取 父级路径
        //public File getParentFile():获取 父级路径

        //4. 获取 文件 大小:
        //public long getFreeSpace():可用
        //public long getUsableSpace():已使用
        //public long getTotalSpace():总

        //5. 修改 文件/路径 属性:
        //public boolean setLastModified(long time):设置最后修改时间
        //public boolean setReadOnly():设置只读
        //public boolean setWritable(boolean writable, boolean ownerOnly):设置可写
        //public boolean setReadable(boolean readable, boolean ownerOnly):设置可读

        File parentFile1 = path.getParentFile();
        System.out.println(parentFile1); //E:\test (parentFile1.toString())
        File parentFile = file.getParentFile();
        System.out.println(parentFile); //E:\test\child (parentFile.toString())

        String parent1 = path.getParent();
        System.out.println(parent1); //E:\test\child
        String parent = file.getParent();
        System.out.println(parent); //E:\test

        File absoluteFile = file.getAbsoluteFile();
        System.out.println(absoluteFile); //E:\test\child\TestFile.java ((absoluteFile.toString()))
        String absolutePath = path.getAbsolutePath();
        System.out.println(absolutePath); //E:\test\child

        String path1 = file.getPath();
        System.out.println(path1); //E:\test\child\TestFile.java
        String path2 = path.getPath();
        System.out.println(path2); //E:\test\child

        String fileName = file.getName();
        System.out.println(fileName); //TestFile.java
        String fileName1 = path.getName();
        System.out.println(fileName1); //child

        //apache的 FileUtils 类
    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值