JavaSE 进阶(十六)

File

表示物理磁盘中的一个文件或者文件夹。

方法名说明
boolean CreateNewFile()创建一个空文件。
boolean mkdir()创建一个指定路径名的文件夹。
boolean delete()删除一个指定的文件或文件夹,文件夹必须为空才能被删除。
boolean exists()判断指定的文件或文件夹是否存在。
String getAbsolutePath()返回文件或文件夹的绝对路径
String getName()返回文件或文件夹的名称
String getParent()返回文件/文件夹所在路径
boolean isDirectory()判断是否为目录
boolean isFile()判断是否为文件
long length()返回文件的长度
File[] listFiles()返回目录中的所有内容,一个文件数组。如果指定的路径不是一个目录就返回 null。
boolean renameTo(File dest)重命名指定的文件。

文件操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package ml.guest997;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;

public class FileTest {
public static void main(String[] args) throws IOException {
System.out.println(File.separator); //名称分隔符
System.out.println(File.pathSeparator); //路径分隔符

File file = new File("../IO/test.md"); //创建文件对象
if (!file.exists()) {
file.createNewFile();
FileWriter fw = new FileWriter("test.md", true); //设置为追加内容
fw.write("https://guest997.ml - 一个在互联网下的小小 Coder");
fw.close();
}
System.out.println(file.length()); //返回的是文件字节数
System.out.println(new Date(file.lastModified()).toLocaleString()); //获取文件最后修改时间
System.out.println(file.isFile());
System.out.println(file.renameTo(new File("rename.md")));
//即使文件被重命名了,下面方法显示的文件名依旧是在创建文件对象时传入的文件名。
System.out.println(file.getPath());
System.out.println(file.getAbsolutePath());
System.out.println(file.getName());
System.out.println(file.getParent()); //只有在 new File 时,存在父路径,才能正常显示,否则为 null。
System.out.println(file.delete()); //重命名之后会使得删除文件失败
}
}
/*结果为
\
;
58
2022-3-12 20:14:32
true
true
..\IO\test.md
E:\DEMO\IO\..\IO\test.md
test.md
..\IO
false
*/

文件夹操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package ml.guest997;

import java.io.File;
import java.util.Arrays;
import java.util.Date;

public class FolderTest {
public static void main(String[] args) {
File dir = new File("../IO/d1/d2");
if (!dir.exists()) {
//file.mkdir(); //只能创建单级目录
dir.mkdirs();
}
System.out.println(dir.getName());
System.out.println(dir.getPath());
System.out.println(dir.getAbsolutePath());
System.out.println(dir.getParent());
System.out.println(new Date(dir.lastModified()).toLocaleString());
System.out.println(dir.isDirectory());

//遍历目录
File file = new File("E:/Demo/IO/target");
//方式一
String[] list = file.list();
System.out.println(Arrays.toString(list));
//方式二
File[] files = file.listFiles();
for (File f : files) {
System.out.println(f);
}

System.out.println(dir.delete()); //只能删除空的最后一级文件夹目录
}
}
/*结果为
d2
..\IO\d1\d2
E:\DEMO\IO\..\IO\d1\d2
..\IO\d1
2022-3-12 21:02:26
true
[classes, generated-sources]
E:\Demo\IO\target\classes
E:\Demo\IO\target\generated-sources
true
*/

文件过滤器(FileFilter 接口)

boolean accept(File pathname):当调用 File 类中的 listFiles 方法时,支持传入 FileFilter 接口实现类,对获取的文件进行过滤,只有满足条件的文件才可以出现在返回值中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package ml.guest997;

import java.io.File;

public class FF {
public static void main(String[] args) {
File dir = new File("../IO");
File[] files = dir.listFiles(pathname -> pathname.getName().endsWith(".md"));
for (File file : files) {
System.out.println(file);
}
}
}
//结果为 ..\IO\rename.md

递归遍历和删除文件夹

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package ml.guest997;

import java.io.File;

public class RecursiveFolder {
public static void main(String[] args) {
traverseFolder(new File(".idea"));
deleteFolder(new File("target"));
}

//递归遍历文件夹
public static void traverseFolder(File dir) {
File[] files = dir.listFiles();
if (files != null && files.length > 0) {
for (File file : files) {
if (file.isDirectory()) {
traverseFolder(file);
} else {
System.out.println(file.getAbsolutePath());
}
}
}
}

//递归删除文件夹
public static void deleteFolder(File dir) {
File[] files = dir.listFiles();
if (files != null && files.length > 0) {
for (File file : files) {
if (file.isDirectory()) {
deleteFolder(file);
} else {
file.delete();
}
}
}
dir.delete();
}

}
/*结果为
E:\DEMO\IO\.idea\.gitignore
E:\DEMO\IO\.idea\compiler.xml
E:\DEMO\IO\.idea\encodings.xml
E:\DEMO\IO\.idea\inspectionProfiles\Project_Default.xml
E:\DEMO\IO\.idea\jarRepositories.xml
E:\DEMO\IO\.idea\misc.xml
E:\DEMO\IO\.idea\runConfigurations.xml
E:\DEMO\IO\.idea\workspace.xml
*/

Properties

以键值对的形式存储属性名和属性值。属性名和属性值都是字符串类型。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package ml.guest997;

import java.io.*;
import java.util.Map;
import java.util.Properties;

public class PropertiesTest {
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
//设置的属性最好不要有中文
properties.setProperty("Blog", "https://guest997.ml");
properties.setProperty("Guest997", "一个在互联网下的小小 Coder");

//遍历
for (Object key : properties.keySet()) {
System.out.println(key);
}
for (Object value : properties.values()) {
System.out.println(value);
}

for (Map.Entry<Object, Object> kv : properties.entrySet()) {
System.out.println(kv.getKey());
System.out.println(kv.getValue());
}

for (String propertyName : properties.stringPropertyNames()) {
System.out.println(propertyName + ":" + properties.getProperty(propertyName));
}

//通过流读取和写入
PrintWriter pw = new PrintWriter("print.md");
properties.list(pw);
pw.close();

//保存
FileWriter fw = new FileWriter("test.properties");
properties.store(fw, "注释");
fw.close();

//加载
Properties properties2 = new Properties();
FileReader fr = new FileReader("test.properties");
properties2.load(fr);
fr.close();
System.out.println(properties2);
}
}
/*结果为
Guest997
Blog
一个在互联网下的小小 Coder
https://guest997.ml
Guest997
一个在互联网下的小小 Coder
Blog
https://guest997.ml
Guest997:一个在互联网下的小小 Coder
Blog:https://guest997.ml
{Guest997=一个在互联网下的小小 Coder, Blog=https://guest997.ml}
*/

print.md

1
2
3
-- listing properties --
Guest997=一个在互联网下的小小 Coder
Blog=https://guest997.ml

test.properties

1
2
3
4
#\u6CE8\u91CA
#Sat Mar 12 22:16:37 CST 2022
Guest997=一个在互联网下的小小 Coder
Blog=https\://guest997.ml