介绍
在Java中我们经常会处理文件的操作,下面来看看Java中文件操作类File的使用吧
构造方法
File(File parent, Stringchild): 根据父目录 + 孩子文件路径,创建一个新的 File 实例
File(String pathname): 根据文件路径创建一个新的 File 实例,路径可以是绝对路径或者相对路径
File(String parent, Stringchild): 根据父目录 + 孩子文件路径,创建一个新的 File 实例,父目录用路径表示
普通方法
获取路径
String getParent() 返回 File 对象的父目录文件路径
String getName() 返回 FIle 对象的纯文件名称
String getPath() 返回 File 对象的文件路径
String getAbsolutePath() 返回 File 对象的绝对路径
String getCanonicalPath() 返回 File 对象的修饰过的绝对路径
判断类型
boolean exists() 判断 File 对象描述的文件是否真实存在
boolean isDirectory() 判断 File 对象代表的文件是否是一个目录
boolean isFile() 判断 File 对象代表的文件是否是一个普通文件
创建删除
boolean createNewFile() 根据 File 对象,自动创建一个空文件。成功创建后返回 true
boolean delete() 根据 File 对象,删除该文件。成功删除后返回 true
void deleteOnExit() 根据 File 对象,标注文件将被删除,删除动作会到JVM 运行结束时才会进行
文件列表
String[] list() 返回 File 对象代表的目录下的所有文件名
File[] listFiles() 返回 File 对象代表的目录下的所有文件,以 File 对象表示
boolean mkdir() 创建 File 对象代表的目录
boolean mkdirs() 创建 File 对象代表的目录,如果必要,会创建中间目录
文件属性
boolean renameTo(Filedest) 进行文件改名,也可以视为我们平时的剪切、粘贴操作
boolean canRead() 判断用户是否对文件有可读权限
boolean canWrite() 判断用户是否对文件有可写权限
测试代码
package com.example.java.test;
import java.io.File;
import java.io.IOException;
/**
* @description: File Operator
* @author: zj
* @date: 2022-07-20 10:33
*/
public class FileOperatorTest {
public static void main(String[] args) throws IOException {
//part1
File file1 = new File("test.txt");
System.out.println("file1.createNewFile() : " + file1.createNewFile());
System.out.println("file1.getParent() : " + file1.getParent());
System.out.println("file1.getName() : " + file1.getName());
System.out.println("file1.getPath() : " + file1.getPath());
System.out.println("file1.getAbsolutePath() : " + file1.getAbsolutePath());
System.out.println("file1.getCanonicalPath() : " + file1.getCanonicalPath());
System.out.println("====part1 end====\n");
//part2
File file2 = new File("text.txt");
System.out.println("file2.exists() : " + file2.exists());
System.out.println("file2.isDirectory() : " + file2.isDirectory());
System.out.println("file2.isFile() : " + file2.isFile());
System.out.println("file2.createNewFile() : " + file2.createNewFile());
System.out.println("file2.exists() : " + file2.exists());
System.out.println("file2.isDirectory() : " + file2.isDirectory());
System.out.println("file2.isFile() : " + file2.isFile());
System.out.println("====part2 end====\n");
//part3
File file3 = new File("file.txt"); // 要求该文件不存在,才能看到相同的现象
System.out.println("file3.exists() :" + file3.exists());
System.out.println("file3.createNewFile() : " + file3.createNewFile());
System.out.println("file3.exists() : " + file3.exists());
System.out.println("file3.delete() : " + file3.delete());
System.out.println("file3.exists() : " + file3.exists());
System.out.println("====part3 end====\n");
//part4
//这个目录不存在
File file4 = new File("text");
System.out.println("file4.isFile() : " + file4.isFile());
System.out.println("file4.isDirectory() : " + file4.isDirectory());
System.out.println("file4.mkdir() : " + file4.mkdir());
System.out.println("file4.isFile() : " + file4.isFile());
System.out.println("file4.isDirectory() : " + file4.isDirectory());
System.out.println("====part4 end====\n");
//part5
File file5 = new File("text");
File dest5 = new File("dest");
System.out.println("file5.exists() : " + file5.exists());
System.out.println("dest5.exists() : " + dest5.exists());
file5.renameTo(dest5);
System.out.println("file5.exists() : " + file5.exists());
System.out.println("dest5.exists() : " + dest5.exists());
System.out.println("====part5 end====\n");
}
}
测试效果
file1.createNewFile() : true
file1.getParent() : null
file1.getName() : test.txt
file1.getPath() : test.txt
file1.getAbsolutePath() : D:\Workspace\java\test.txt
file1.getCanonicalPath() : D:\Workspace\java\test.txt
====part1 end====
file2.exists() : false
file2.isDirectory() : false
file2.isFile() : false
file2.createNewFile() : true
file2.exists() : true
file2.isDirectory() : false
file2.isFile() : true
====part2 end====
file3.exists() :false
file3.createNewFile() : true
file3.exists() : true
file3.delete() : true
file3.exists() : false
====part3 end====
file4.isFile() : false
file4.isDirectory() : false
file4.mkdir() : true
file4.isFile() : false
file4.isDirectory() : true
====part4 end====
file5.exists() : true
dest5.exists() : false
file5.exists() : false
dest5.exists() : true
====part5 end====