File类
概述
File文件和路径名的抽象表示形式
graph TD
A[File]
B[路径]
A --> C[相对路径]
A --> D[绝对路径]
构造方法
- File(String pathname) 根据一个路径得到File对象
- FIle(String parent, String child) 根据一个目录和一个子文件/目录得到一个File对象
- File(FIle parent, String child) 根据一个父File对象和一个子文件/目录得到File对象
- File(URI uri) 通过将给定的file:URI转化为抽象路径名来创建新的File对象
import java.io.File;
public class DemoFile {
public static void main(String[] args) {
File file = new File("/home/mephisto/eclipse-workspace/learnjava/src/com/mephisto/file/demo.txt");
System.out.println(file.exists());
File file2 = new File("/home/mephisto/eclipse-workspace/learnjava/src/com/mephisto/file/","demo.txt");
System.out.println(file2.exists());
File parent = new File("/home/mephisto/eclipse-workspace/learnjava/src/com/mephisto/file/");
String child = "demo.txt";
System.out.println(new File(parent,child).exists());
}
}
创建功能
- public boolean createNewFile(): 创建文件 如果存在这样的文件就不创建
- public boolean mkdir() : 创建文件夹, 如果存在这样的文件夹, 就不创建
- public boolean madirs(): 创建文件夹, 如果父文件夹不存在,会创建出来, 类似递归创建
import java.io.File;
import java.io.IOException;
public class DemoFildMethod {
public static void main(String[] args) throws IOException {
String pathname = "/home/mephisto/eclipse-workspace/learnjava/src/com/mephisto/file";
File file = new File(pathname + "test");
file.createNewFile();
File file2 = new File(pathname + "demo");
file2.mkdir();
File file3 = new File(pathname + "demotest/test");
file3.mkdirs();
}
}
重命名和删除功能
方法
- public boolean rename(File dest) 把文件重命名为指定的文件路径
- public boolean delete() 删除文件或文件夹
重命名注意事项
- 如果路径名相同, 就是改名
- 如果路径名不同, 就是改名并剪切
删除注意事项
- Java中的删除不走回收站
- 要删除一个文件夹, 注意该文件夹下不能包含文件或者文件夹
import java.io.File;
public class DemoDeleteOrRename {
public static void main(String[] args) {
String pathname = "/home/mephisto/eclipse-workspace/learnjava/src/com/mephisto/file";
File file = new File(pathname,"test");
file.delete();
File file3 = new File(pathname,"demotest");
System.out.println(file3.delete()); // false
File file2 = new File(pathname,"demo.txt");
file2.renameTo(new File(pathname,"Demo"));
}
}
判断功能
方法
- public boolean isDirectory() : 判断是否是目录
- public boolean isFile() : 判断是否是文件
- public boolean exists() : 判断是否存在
- public boolean canRead() : 判断是否可读
- public boolean canWrite() : 判断是否可写
- public boolean isHidden() : 判断是否隐藏
import java.io.File;
public class DemoJudge {
public static void main(String[] args) {
String pathname = "/home/mephisto/eclipse-workspace/learnjava/src/com/mephisto/file";
File file = new File(pathname);
System.out.println(file.isDirectory());
File file2 = new File(pathname,"Demo");
System.out.println(file2.canRead());
System.out.println(file2.canWrite());
// windows系统认为所有文件都是可读的, 不可以设置是否可读, 可以设置是否可写
file2.setReadable(false);
System.out.println(file2.canRead());
file2.setWritable(false);
System.out.println(file2.canWrite());
file2.setReadable(true);
file2.setWritable(true);
file2.renameTo(new File(pathname,".Demo"));
File file3 = new File(pathname,".Demo");
System.out.println(file3.isHidden());
File file4 = new File(pathname,".Demo");
file4.renameTo(new File(pathname,"Demo"));
}
}
获取功能
方法
- public String getAbsolutePath() 获取绝对路径
- public String getPath() 获取路径
- public String getName() 获取名称
- public long lenth() 获取字节数
- pubic long lastModified() 获取最后一次修改的时间,毫秒值
- public String[] list() 获取指定目录下的所有文件或者文件夹的名称数组
- public File[] listFiles() 获取指定目录下的所有文件或者文件夹的File数组
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DemoGetMethod {
public static void main(String[] args) {
String pathname = "/home/mephisto/eclipse-workspace/learnjava/src/com/mephisto/file";
File file = new File(pathname, "Demo");
System.out.println(file.getPath());
System.out.println(file.getName());
System.out.println(file.getAbsolutePath());
System.out.println(file.length());
System.out.println(new SimpleDateFormat("yyyy年MM月dd日HH:mm:ss").format(new Date(file.lastModified())));
String dirName = "/home/mephisto/eclipse-workspace/learnjava/src/com/mephisto/file";
File file2 = new File(dirName);
for (String string : file2.list()) {
System.out.println(string);
}
System.out.println("===============");
for (File file3 : file2.listFiles()) {
System.out.println(file3.getName());
}
}
}
文件名称过滤器
方法
- public String[] list(FilenameFilter filter)
- public File[] listFiles(FileFilter filter)