• java编程基础总结——26.File对象


    File对象
        java封装的一个操作文件及文件夹(目录)的对象。

       可以操作磁盘上的任何一个文件和文件夹

    一、 构造方法

    1. 传字符串的路径

    2. URI是和网络相关的接口。构建的是网络文件

    3. File对象,再加一个子字符串

    4. 字符串 字符串(可以把字符串构建成File对象,也可以把File对象用字符串表示)

        使用场景:文件和文件夹分离(某个文件的存储目录和文件名称分离),文件名称写子目录也                          可以,但没什么意义,直接用第一个就OK了

    注意:windows支持\,但是一个\在java字符串中有转义的意思,如\r,\n,所以用两个\表示一个\

    1. @Test
    2. void testFileCreate() {
    3. // File file = new File("C:\\a.jpg");第一个构造 绝对路径
    4. // File file = new File(new File("c:\\"), "a.jpg");第三个构造
    5. File file = new File("c:/", "a.jpg");//第四个构造
    6. System.out.println(file.exists());
    7. }


    绝对路径和相对路径:
        绝对路径:直接可以定位文件的一种路径
            window系统:直接使用盘符找找: d:/a/b/c.jpg
            linux: /(根)       /usr/local/bin/java
            网络: http://www.baidu.com/a.jpg


        相对路径:相对于某个参照物(相对于文件夹),进行查找

     

     二、常见方法

    1. createNewFile()、exists()

        exists()判断文件是否存在

        createNewFile()文件不存在的时候创建文件,如果已经存在,再次创建不会刷新(覆盖)文件 

    1. @Test
    2. void testFileCreate02() throws Exception {
    3. // File f = new File("./a.txt");相对路径
    4. File f = new File("a.txt");
    5. // 判断文件是否存在
    6. if (!f.exists()) {
    7. // 创建文件
    8. f.createNewFile();
    9. System.out.println("创建成功");
    10. }
    11. }

    1. @Test
    2. void testFileCreate03() throws Exception {
    3. File f = new File("a.txt");
    4. System.out.println(f.createNewFile());
    5. }

    2. canExecute()、canWrite()、canRead()、compareTo()

     返回0是相等

    1. @Test
    2. void testFile02() throws Exception {
    3. File file = new File("a.txt");
    4. // 是否存在执行权限
    5. System.out.println(file.canExecute());
    6. // 是否有写的权限
    7. System.out.println(file.canWrite());
    8. // 是否有读的权限
    9. System.out.println(file.canRead());
    10. System.out.println(file.compareTo(new File("a.txt")));
    11. }

     3. delete()、deleteOnExit()

    //删除文件(a.txt会被删除)

    file.delete()

     //已经删除了,再次删除会返回false

    System.out.println(file.delete());

     

    // 在JVM退出时删除文件

    //比如java自身的缓存文件,程序启动的时候临时缓存在磁盘上,程序运行结束删掉

    file.deleteOnExit(); 

    4.getAbsoluteFile()、getAbsolutePath()

    1. @Test
    2. void testFile02() throws Exception {
    3. File file = new File("a.txt");
    4. file.createNewFile();
    5. // 将文件对象由相对路径转换为绝对路径
    6. File absoluteFile = file.getAbsoluteFile();
    7. System.out.println(absoluteFile);
    8. }

    1. @Test
    2. void testFile02() throws Exception {
    3. File file = new File("a.txt");
    4. file.createNewFile();
    5. // 获取文件的真实路径
    6. String path = file.getAbsolutePath();
    7. System.out.println(path);

     5. getFreeSpace()、getUsableSpace()、getTotalSpace()

    1. @Test
    2. void testFile02() throws Exception {
    3. File file = new File("a.txt");
    4. // 空闲空间(磁盘空间)
    5. System.out.println((file.getFreeSpace() / 1024 / 1024) +"M");//字节->k->M
    6. // 可有空间
    7. System.out.println((file.getUsableSpace() / 1024 / 1024 / 1024) +"G");
    8. // 总空间
    9. System.out.println((file.getTotalSpace() / 1024 / 1024 / 1024) +"G");
    10. //当前文件占用
    11. System.out.println(file.length());
    12. }

     

     6. length()

    1. @Test
    2. void testFile02() throws Exception {
    3. File file = new File("a.txt");
    4. //当前文件占用
    5. System.out.println(file.length());
    6. }

    7. getName()、getParent()、getParentFile()、getPath()

    名字+目录-->完整的相对路径

    getAbsolutePath() = getPath() + getParent()

    我们是用相对路径构造的,getParent()拿不到父级的相对路径。转成绝对路径就可以了

    1. @Test
    2. void testFile02() throws Exception {
    3. File file = new File("a.txt");
    4. System.out.println(file.getName());
    5. System.out.println(file.getParent());
    6. }

    1. @Test
    2. void testFile02() throws Exception {
    3. File file = new File("a.txt");
    4. // 将文件对象由相对路径转换为绝对路径
    5. File absoluteFile = file.getAbsoluteFile();
    6. System.out.println(absoluteFile);
    7. System.out.println(absoluteFile.getName());
    8. // 获取父级的路径等操作,必须使用绝对路径对象
    9. System.out.println(absoluteFile.getParent());
    10. //拿到父类对象
    11. System.out.println(absoluteFile.getParentFile());
    12. System.out.println(file.getPath());
    13. }

     8. isAbsolute()、isDirectory()、isFile()、isHidden()

    1. @Test
    2. void testFile02() throws Exception {
    3. File file = new File("a.txt");
    4. // 以is开头的几个方法,都是判断是否符合某种规则
    5. //是不是绝对路径构建的对象
    6. System.out.println(file.isAbsolute());
    7. //是不是文件夹
    8. System.out.println(file.isDirectory());
    9. //是不是文件
    10. System.out.println(file.isFile());
    11. //是不是隐藏文件
    12. System.out.println(file.isHidden());
    13. }

     9. mkdir()、mkdirs()

          mkdir()只能创建一层目录

           mkdirs()能创建多层目录

    1. @Test
    2. void test03() {
    3. File file = new File("./lib");
    4. //只能创建一层目录
    5. if (file.mkdir()) {
    6. System.out.println("目录创建成功");
    7. } else {
    8. System.out.println("创建失败");
    9. }
    10. }

    1. @Test
    2. void test03() {
    3. File file = new File("config/resources");
    4. //只能创建一层目录
    5. if (file.mkdir()) {
    6. System.out.println("目录创建成功");
    7. } else {
    8. System.out.println("创建失败");
    9. }
    10. }

     

    1. @Test
    2. void test03() {
    3. File file = new File("config/resources");
    4. // 使用递归的方式,完成多层目录的创建
    5. if (file.mkdirs()) {
    6. System.out.println("目录创建成功");
    7. } else {
    8. System.out.println("创建失败");
    9. }
    10. }

     

    10. renameTo()

    1. @Test
    2. void test04() {
    3. File file = new File("a.txt");
    4. // 剪贴、复制、挪动文件的作用
    5. System.out.println(file.renameTo(new File("d:\\b.txt")));
    6. }

     

    11. list()、listFiles()

     以字符串数组的形式返回当前目录下的所有文件(只打印第一层)

    1. @Test
    2. void test06() {
    3. File file = new File("D:\\javaSE\\note");
    4. //获取当前目录下的所有文件名称
    5. String[] names = file.list();
    6. for (String s : names) {
    7. System.out.println(s);
    8. }
    9. }

    1. @Test
    2. void test06() {
    3. File file = new File("./");
    4. //获取当前目录下的所有文件对象
    5. File[] names = file.listFiles();
    6. for (File f : names) {
    7. System.out.println(f.getName());
    8. }
    9. }

    1. @Test
    2. void test06() {
    3. File file = new File("D:\\javaSE\\note");
    4. // 过滤特定的文件
    5. //lambda表达式
    6. String[] names = file.list((dir, name) -> name.endsWith(".txt"));
    7. for (String s : names) {
    8. System.out.println(s);
    9. }
    10. }

    1. @Test
    2. void test07() {
    3. File file = new File("D:\\\\javaSE\\\\note");
    4. // 过滤特定的文件
    5. File[] names = file.listFiles(pathname -> pathname.getName().endsWith(".txt"));
    6. for (File f : names) {
    7. System.out.println(f.getAbsolutePath());
    8. }
    9. }

    应用:

    展示磁盘所有文件(递归遍历磁盘 ):

    1. public static void scannFile(String path) {
    2. File file = new File(path);
    3. File[] listFiles = file.listFiles();
    4. for (File f : listFiles) {
    5. if (f.isFile()) {
    6. System.out.println(f.getAbsolutePath());
    7. } else if(f.isDirectory()) {
    8. // 文件夹
    9. scannFile(path + File.separatorChar + f.getName());
    10. }
    11. }
    12. }
    13. public static void main(String[] args) {
    14. scannFile("G:\\");
    15. }

     separatorChar()分隔

  • 相关阅读:
    大数据之Hudi数据湖_版本兼容与Maven安装配置_解决Hudi与Hadoop3.0的兼容问题_编译hudi源码---大数据之Hudi数据湖工作笔记0002
    javaee springMVC 放行静态资源
    【SSM框架】Mybatis详解10(源码自取)之入参、返回值map
    【原创】常用元器件(电阻)选型之阻值有多少-cayden20220910
    vCenter纳管ESXI主机出错
    Prompt 指南之零样本与少样本提示,超详细解析!
    设计模式-创建型-原型模式-Prototype
    牛客竞赛每日俩题 - Day6
    STM32-PWR电源控制
    适用于90%网剧、网大的最新备案流程解析
  • 原文地址:https://blog.csdn.net/m0_58679504/article/details/126279146