File类的使用
java.io.File类是文件和文件目录路径的抽象表示形式,与具体的操作系统无关。
File类提供了新建、删除、重命名文件和目录的功能,但File不能访问文件内容本身以及想文件中写入内容。如果需要访问文件内容本身,则需要使用输入/输出流。通常将File类的实例作为流的构造器参数,然后流就可以读取或写入数据到文件中。
想要在Java程序中表示一个真实存在的文件或目录,那么必须有一个File对象,但是Java程序中的一个File对象,可能没有一个真实存在的文件或目录。
File file = new File("1.txt"); // 当前moudle下没有1.txt文件
System.out.println(file); // 1.txt
常用构造器
Constructor | Description |
---|---|
File(File parent, String child) |
Creates a new File instance from a parent abstract pathname and a child pathname string. |
File(String pathname) |
Creates a new File instance by converting the given pathname string into an abstract pathname. |
File(String parent, String child) |
Creates a new File instance from a parent pathname string and a child pathname string. |
File(URI uri) |
Creates a new File instance by converting the given file: URI into an abstract pathname. |
以pathname为路径创建File对象,可以是绝对路径或者相对路径,如果pathname是相对路径,则默认的当前路径在系统属性user.dir中存储。(Idea编辑器中,在main方法里,相对路径为当前工程。若是@Test,则为当前的module)
// type1
File file = new File("1.txt");
File file2 = new File("D:\GWX\1.md");
// type2
File file3 = new File("D:\GWX", "1.md");
// type3
File file4 = new File("D:\GWX", "dir");
File file5 = new File(file4, "1.txt");
路径分隔符
路径中的每级目录之间用一个路径分隔符隔开。
- windows中路径分隔符\
- unix中路径分隔符/
Java程序支持跨平台运行,因此路径分隔符要慎用。为了解决这个问题,File提供了一个类常量,
public static final String separator。根据操作系统,动态的提供分隔符
File file = new File("dir1" + File.separator + "1.md");
常用方法
File提供了非常多的方法,并且绝大部分都非常的实用。下面从几个方面来介绍这些常用方法:
属性相关
Modifier and Type | Method | Description |
---|---|---|
File |
getAbsoluteFile() |
Returns the absolute form of this abstract pathname. |
String |
getAbsolutePath() |
Returns the absolute pathname string of this abstract pathname. |
long |
getFreeSpace() |
Returns the number of unallocated bytes in the partition named by this abstract path name. |
String |
getPath() |
Converts this abstract pathname into a pathname string. |
String |
getName() |
Returns the name of the file or directory denoted by this abstract pathname. |
String |
getParent() |
Returns the pathname string of this abstract pathname’s parent, or null if this pathname does not name a parent directory. |
File |
getParentFile() |
Returns the abstract pathname of this abstract pathname’s parent, or null if this pathname does not name a parent directory. |
long |
lastModified() |
Returns the time that the file denoted by this abstract pathname was last modified. |
long |
length() |
Returns the length of the file denoted by this abstract pathname. |
String[] |
list() |
Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname. |
String[] |
list(FilenameFilter filter) |
Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter. |
File[] |
listFiles() |
Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname. |
File[] |
listFiles(FileFilter filter) |
Returns an array of abstract pathnames denoting the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter. |
getPath、getName用法示例:
File file3 = new File("D:\GWX", "1.md");
System.out.println(file3.getPath()); // D:\GWX.md
System.out.println(file3.getName()); // 1.md
File file4 = new File("dir1\dir2", "1.md");
System.out.println(file4.getPath()); // dir1\dir2.md
System.out.println(file4.getName()); // 1.md
重命名
bool renameTo(File dest) 若目标文件不存在,则可以重命名成功,否则,则不会成功。该方法名应该叫move更加合适,因为该方法可以移动文件并修改文件名。
File file = new File("1.txt");
if (file.renameTo(new File("dir/hello.txt"))) {
System.out.println("文件移动成功");
} else {
System.out.println("文件移动失败");
}
判断相关
Modifier and Type | Method | Description |
---|---|---|
boolean |
canExecute() |
Tests whether the application can execute the file denoted by this abstract pathname. |
boolean |
canRead() |
Tests whether the application can read the file denoted by this abstract pathname. |
boolean |
canWrite() |
Tests whether the application can modify the file denoted by this abstract pathname. |
boolean |
exists() |
Tests whether the file or directory denoted by this abstract pathname exists. |
boolean |
isDirectory() |
Tests whether the file denoted by this abstract pathname is a directory. |
boolean |
isFile() |
Tests whether the file denoted by this abstract pathname is a normal file. |
boolean |
isHidden() |
Tests whether the file named by this abstract pathname is a hidden file. |
创建、删除
Modifier and Type | Method | Description |
---|---|---|
boolean |
createNewFile() |
Atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist. |
boolean |
delete() |
Deletes the file or directory denoted by this abstract pathname. |
boolean |
mkdir() |
Creates the directory named by this abstract pathname. |
boolean |
mkdirs() |
Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories. |
练习
习题1:判断指定目录下是否有后缀名为.png的文件,如果有,就输出该文件名称
File dir = new File("dir");
if (!dir.isDirectory()) {
throw new IOException("the file is not a dir");
}
String[] list = dir.list((dirfile, filename)->{
return filename.endsWith(".png");
});
for (String filename : list)
System.out.println(filename);
习题2.遍历指定目录所有文件名称,包括子文件目录中的文件。
@Test
void test6 () throws IOException {
File dir = new File("../IO");
if (!dir.isDirectory()) {
throw new IOException("the file is not a dir");
}
printFile(dir, 0);
}
private static void printFile (File file, int length)
{
for (int i=0; i<length; i++) {
System.out.print(" ");
}
System.out.println(file.getName());
if (file.isDirectory()) {
File[] files = file.listFiles();
for (File f : files) {
printFile(f, length + 2);
}
}
}