Skip to content

Commit 09d3bde

Browse files
committed
feat: 增加文件IO操作
1 parent bcc1c8b commit 09d3bde

4 files changed

Lines changed: 269 additions & 3 deletions

File tree

core-java-modules/core-java-io/pom.xml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,14 @@
2222
<dependency>
2323
<groupId>com.google.guava</groupId>
2424
<artifactId>guava</artifactId>
25-
<version>31.0.1-jre</version>
26-
<type>jar</type>
27-
<scope>provided</scope>
25+
<version>32.1.3-jre</version>
2826
</dependency>
27+
<dependency>
28+
<groupId>commons-io</groupId>
29+
<artifactId>commons-io</artifactId>
30+
<version>2.7</version>
31+
</dependency>
32+
2933
</dependencies>
3034

3135
</project>
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
package com.wdbyte.io.file;
2+
3+
import java.io.BufferedWriter;
4+
import java.io.File;
5+
import java.io.FileOutputStream;
6+
import java.io.FileWriter;
7+
import java.io.IOException;
8+
import java.nio.charset.StandardCharsets;
9+
import java.nio.file.Files;
10+
import java.nio.file.Path;
11+
import java.nio.file.Paths;
12+
import java.nio.file.StandardOpenOption;
13+
import java.util.ArrayList;
14+
import java.util.List;
15+
16+
import com.google.common.base.Charsets;
17+
import com.google.common.io.FileWriteMode;
18+
import org.apache.commons.io.FileUtils;
19+
import org.junit.jupiter.api.Test;
20+
21+
/**
22+
* @author niulang
23+
* @date 2023/12/12
24+
*/
25+
public class FileAppendDemo {
26+
27+
@Test
28+
public void appendLine1() throws IOException {
29+
Path path = Paths.get("/Users/darcy/wdbyte/test1.txt");
30+
// 写入一行数据
31+
Files.write(path, "line append1".getBytes(StandardCharsets.UTF_8),
32+
StandardOpenOption.CREATE, StandardOpenOption.APPEND);
33+
// 写入一个换行符
34+
Files.write(path, System.lineSeparator().getBytes(StandardCharsets.UTF_8),
35+
StandardOpenOption.APPEND);
36+
}
37+
38+
@Test
39+
public void appendLine2() throws IOException {
40+
String path = "/Users/darcy/wdbyte/test1.txt";
41+
try (FileWriter fileWriter = new FileWriter(path, true)) {
42+
fileWriter.write("line append2");
43+
fileWriter.write(System.lineSeparator());
44+
}
45+
}
46+
@Test
47+
public void appendLine3() throws IOException {
48+
String path = "/Users/darcy/wdbyte/test1.txt";
49+
try (FileWriter fileWriter = new FileWriter(path, true)) {
50+
BufferedWriter bw = new BufferedWriter(fileWriter);
51+
bw.write("line append3");
52+
bw.newLine();
53+
}
54+
}
55+
56+
@Test
57+
public void appendLine4() throws IOException {
58+
String path = "/Users/darcy/wdbyte/test1.txt";
59+
try (FileOutputStream fileOutputStream = new FileOutputStream(path, true)) {
60+
fileOutputStream.write("line append4".getBytes(StandardCharsets.UTF_8));
61+
fileOutputStream.write(System.lineSeparator().getBytes(StandardCharsets.UTF_8));
62+
}
63+
}
64+
65+
/**
66+
* java 11 Files.writeString
67+
* @throws IOException
68+
*/
69+
public void appendLineJava11() throws IOException {
70+
Path path = Paths.get("/Users/darcy/wdbyte/test1.txt");
71+
Files.writeString(path, "line appendLineJava11",
72+
StandardOpenOption.CREATE,
73+
StandardOpenOption.APPEND);
74+
}
75+
76+
77+
/**
78+
* Java 8
79+
*
80+
* @throws IOException
81+
*/
82+
@Test
83+
public void appendLineJava8() throws IOException {
84+
Path path = Paths.get("/Users/darcy/wdbyte/test1.txt");
85+
List<String> dataList = new ArrayList<>();
86+
dataList.add("line1 appendLineJava8");
87+
dataList.add("line2 appendLineJava8");
88+
dataList.add("line3 appendLineJava8");
89+
90+
Files.write(path, dataList,
91+
StandardOpenOption.CREATE,
92+
StandardOpenOption.APPEND);
93+
94+
Files.write(path, System.lineSeparator().getBytes(StandardCharsets.UTF_8),
95+
StandardOpenOption.CREATE,
96+
StandardOpenOption.APPEND);
97+
}
98+
99+
@Test
100+
public void appendLineList() throws IOException {
101+
String path = "/Users/darcy/wdbyte/test1.txt";
102+
List<String> dataList = new ArrayList<>();
103+
dataList.add("line1 appendLineList");
104+
dataList.add("line2 appendLineList");
105+
dataList.add("line3 appendLineList");
106+
107+
try (FileWriter fileWriter = new FileWriter(path, true)) {
108+
BufferedWriter bw = new BufferedWriter(fileWriter);
109+
for (String data : dataList) {
110+
bw.write(data);
111+
bw.newLine();
112+
}
113+
}
114+
}
115+
116+
@Test
117+
public void appendByCommonsIo() throws IOException {
118+
File file = new File("/Users/darcy/wdbyte/test1.txt");
119+
String content = "hello commons io,appendByCommonsIo";
120+
FileUtils.writeStringToFile(file, content, StandardCharsets.UTF_8, true);
121+
FileUtils.writeStringToFile(file, System.lineSeparator(), StandardCharsets.UTF_8, true);
122+
}
123+
124+
@Test
125+
public void appendListByCommonsIo() throws IOException {
126+
File file = new File("/Users/darcy/wdbyte/test1.txt");
127+
List<String> dataList = new ArrayList<>();
128+
dataList.add("line1 hello commons io,appendListByCommonsIo");
129+
dataList.add("line2 hello commons io,appendListByCommonsIo");
130+
dataList.add("line3 hello commons io,appendListByCommonsIo");
131+
FileUtils.writeLines(file, dataList, true);
132+
}
133+
134+
@Test
135+
public void appendData(){
136+
// 创建File对象指向要追加内容的文件
137+
File file = new File("/Users/darcy/wdbyte/test1.txt");
138+
// 要追加的内容
139+
String contentToAppend = "Hello, Guava!";
140+
try {
141+
// 使用Guava的Files类以追加模式写入内容
142+
com.google.common.io.Files.asCharSink(file, Charsets.UTF_8, FileWriteMode.APPEND)
143+
.write(contentToAppend);
144+
145+
// 如果需要追加新行,可以使用下面的代码
146+
com.google.common.io.Files.asCharSink(file, Charsets.UTF_8, com.google.common.io.FileWriteMode.APPEND)
147+
.write(contentToAppend + System.lineSeparator());
148+
System.out.println("Content appended successfully.");
149+
} catch (IOException e) {
150+
System.err.println("Error appending content to file: " + e.getMessage());
151+
}
152+
}
153+
154+
@Test
155+
public void appendDataListByGuava(){
156+
// 创建File对象指向要追加内容的文件
157+
File file = new File("/Users/darcy/wdbyte/test1.txt");
158+
159+
// 要追加的内容
160+
List<String> dataList = new ArrayList<>();
161+
dataList.add("line1 hello commons io,appendDataListByGuava");
162+
dataList.add("line2 hello commons io,appendDataListByGuava");
163+
dataList.add("line3 hello commons io,appendDataListByGuava");
164+
165+
try {
166+
// 使用Guava的Files类以追加模式写入内容
167+
com.google.common.io.Files.asCharSink(file, Charsets.UTF_8, FileWriteMode.APPEND)
168+
.writeLines(dataList);
169+
170+
System.out.println("Content appended successfully.");
171+
} catch (IOException e) {
172+
System.err.println("Error appending content to file: " + e.getMessage());
173+
}
174+
}
175+
176+
177+
178+
179+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.wdbyte.io.file;
2+
3+
import java.io.File;
4+
import java.nio.file.Files;
5+
import java.nio.file.Path;
6+
import java.nio.file.Paths;
7+
8+
import org.apache.commons.io.FileUtils;
9+
import org.junit.jupiter.api.Test;
10+
11+
/**
12+
* @author niulang
13+
* @date 2023/12/18
14+
*/
15+
public class FileDelete {
16+
17+
@Test
18+
public void deleteFile() {
19+
Path path = Paths.get("/Users/darcy/wdbyte/test.txt");
20+
try {
21+
Files.delete(path);
22+
System.out.println("文件删除成功");
23+
} catch (Exception e) {
24+
System.out.println("文件删除失败");
25+
e.printStackTrace();
26+
}
27+
}
28+
29+
@Test
30+
public void deleteIfExists() {
31+
Path path = Paths.get("/Users/darcy/wdbyte/test.txt");
32+
try {
33+
boolean deleted = Files.deleteIfExists(path);
34+
if (deleted) {
35+
System.out.println("文件删除成功");
36+
} else {
37+
System.out.println("文件不存在或者无法删除");
38+
}
39+
} catch (Exception e) {
40+
System.out.println("文件删除失败");
41+
e.printStackTrace();
42+
}
43+
}
44+
45+
@Test
46+
public void delete() {
47+
File file = new File("/Users/darcy/wdbyte/test.txt");
48+
if (file.delete()) {
49+
System.out.println("文件删除成功");
50+
} else {
51+
System.out.println("文件删除失败");
52+
}
53+
}
54+
55+
@Test
56+
public void deleteOnExit() {
57+
File file = new File("/Users/darcy/wdbyte/test.txt");
58+
file.deleteOnExit();
59+
}
60+
61+
@Test
62+
public void forceDelete() {
63+
File file = new File("/Users/darcy/wdbyte/test.txt");
64+
try {
65+
FileUtils.forceDelete(file);
66+
System.out.println("文件删除成功");
67+
} catch (Exception e) {
68+
System.out.println("文件删除失败");
69+
e.printStackTrace();
70+
}
71+
}
72+
73+
}

core-java-modules/core-java-io/src/main/java/com/wdbyte/io/file/FileReadDemo.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.util.Scanner;
1616
import java.util.stream.Stream;
1717

18+
import org.apache.commons.io.FileUtils;
1819
import org.junit.jupiter.api.Test;
1920

2021
/**
@@ -165,4 +166,13 @@ public void test8() {
165166
e.printStackTrace();
166167
}
167168
}
169+
170+
@Test
171+
public void readFileByApacheCommons() throws IOException {
172+
File file = new File("/Users/darcy/wdbyte/test.txt");
173+
List<String> list = FileUtils.readLines(file, StandardCharsets.UTF_8);
174+
for (String data : list) {
175+
System.out.println(data);
176+
}
177+
}
168178
}

0 commit comments

Comments
 (0)