编程随笔-Java | 02.File类常用API

常用API介绍

构造方法

不推荐直接在路径字符串中写 “\” ,因为部分操作系统无法识别。

更好的做法是使用静态常量 File.separator,在下文会有介绍。

public File(String filePath){}
public File(String parent, String child){}
public File(File parent, String child){}

使用案例如下:

// 1.根据路径创建对象
String absPath = "E:\abc\def\file.txt";			// 绝对路径
File file1 = new File(absPath);

String relPath = "abc\def\file.txt";		    // 相对路径,此时会在这个路径的前面拼接项目根目录
File file2 = new File(relPath);
// 2.根据两段路径创建对象(parent和child),会自动实现拼接
String dirAbsPath = "E:\abc\def";
String fileName = "file.txt";
File file3 = new File(dirAbsPath, fileName);
// 3.根据File对象和文件名创建对象
String dirAbsPath = "E:\abc\def";
File dir = new File(dirAbsPath);
String fileName = "file.txt";
File file4 = new File(dir, fileName);

separator - 【静态常量】分隔符

不同操作系统的分隔符都有所不同,直接在字符串中以 “\” 作为分隔符是不明智的。使用静态常量 File.separator 时,程序会根据操作系统的不同,自动转换成有效的分隔符。

public static String separator;

getName( ) - 获取当前File名

public String getName();	// 获取当前文件名或文件夹名

getCanonicalPath( ) - 获取当前File路径

public String getPath();			// 获取构造对象时使用的path参数
public String getAbsolutePath();	// 获取绝对路径,但是会有"."
public String getCanonicalPath();	// 获取绝对路径

getAbsolutePath( )获取的绝对路径可能会存在"…/“或”."等表示相对路径的字符串,

getCanonicalPath( )会用真实路径将其进行替换,因此更推荐使用本方法

getLastModified( ) - 获取最后操作的时间戳

public long getLastModified();		// 获取最后操作的时间戳

getParent( ) - 获取父级File名(文件夹名)

public String getParent();

getParentFile( ) - 获取父级File(文件夹对应的File)

public File getParentFile();

exists( ) - 判断是否存在

public boolean exixts();

createNewFile( ) - 创建文件

public boolean createNewFile()		// 成功返回true,若已存在返回false,父级文件夹不存在则抛出异常

mkdirs( ) - 创建文件夹

public boolean mkdir();		// 成功返回true,若已存在返回false,父级文件夹不存在则抛出异常
public boolean mkdirs();	// 联创建文件夹,若已存在返回false

delete( ) - 删除文件 / 删除文件夹

public boolean delete();	// 成功返回true

renameTo( ) - 重命名 / 移动

public boolean renameTo(File dest)			// 传入的参数在同一文件夹,则为重命名;不同文件夹,则为移动

使用案例

目标如下:

  • 级联创建"E:\abc\def"目录
  • 在此目录下创建名为"testFile.txt"的文件
  • 将该文件重命名为"renamedFile.txt"
  • 将该文件其移动至"E:\abc\def"
  • 删除文件夹"E:\abc\def"
package com.xyx_eshang.jdkapidemo.demos;

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

/**
 * @author xyx-Eshang
 */
public class FileDemo {
    public static void testFile() throws IOException {
        // 1. 级联创建文件夹
        String dirPath = "E:" + File.separator + "abc" + File.separator + "def";
        File dir = new File(dirPath);
        System.out.println(dir.mkdirs() ? "成功创建文件夹" : "文件夹已存在");

        // 2. 创建文件
        String fileName = "testFile.txt";
        File file = new File(dirPath, fileName);
        System.out.println(file.createNewFile() ? "成功创建文件" : "文件已存在");

        // 3. 重命名文件
        String newFileName = "renamedFile.txt";
        File newFile = new File(dirPath, newFileName);
        System.out.println(file.renameTo(newFile) ? "成功重命名文件" : "文件不存在");

        // 4. 移动文件
        String newDirPath = "E:" + File.separator + "abc";
        File removedFile = new File(newDirPath, newFileName);
        System.out.println(newFile.renameTo(removedFile) ? "成功移动文件" : "移动文件失败");

        // 5. 删除文件夹
        System.out.println(dir.delete() ? "成功删除文件夹" : "删除文件夹失败");

    }

    public static void main(String[] args) throws IOException {
        testFile();
    }
}

image-20220727012644506

参考文章

Java File类 - 菜鸟教程

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值