Skip to content

Commit f1f12df

Browse files
committed
feat: [Java 创建和写入文件](https://www.wdbyte.com/java/io/file-create-write/)
1 parent d3e0d82 commit f1f12df

5 files changed

Lines changed: 133 additions & 3 deletions

File tree

core-java-modules/core-java-base/src/main/java/com/wdbyte/collection/ArrayListTest4.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.ArrayList;
44
import java.util.Arrays;
55
import java.util.HashMap;
6+
import java.util.Iterator;
67
import java.util.List;
78
import java.util.Map;
89
import java.util.function.Function;
@@ -31,6 +32,18 @@ public static void main(String[] args) {
3132
}
3233
System.out.println(dogMap.get("大黄"));
3334

35+
List<String> arrayList = new ArrayList();
36+
arrayList.add("a");
37+
arrayList.add("b");
38+
39+
Iterator<String> iterator = arrayList.iterator();
40+
while (iterator.hasNext()) {
41+
String next = iterator.next();
42+
if ("a".equals(next)) {
43+
iterator.remove();
44+
}
45+
}
46+
System.out.println(arrayList);
3447
}
3548

3649
}

core-java-modules/core-java-io/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
当前模块包含 IO 相关代码
33

44
### 相关文章
5-
5+
- [Java 创建和写入文件](https://www.wdbyte.com/java/io/file-create-write/)
66
- [字符图案,我用字符画个冰墩墩](https://www.wdbyte.com/java/char-image.html)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
<packaging>jar</packaging>
1515

1616
<properties>
17-
<maven.compiler.source>1.8</maven.compiler.source>
18-
<maven.compiler.target>1.8</maven.compiler.target>
17+
<maven.compiler.source>21</maven.compiler.source>
18+
<maven.compiler.target>21</maven.compiler.target>
1919
</properties>
2020

2121
<dependencies>
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package com.wdbyte.io.file;
2+
3+
import java.io.BufferedWriter;
4+
import java.io.FileWriter;
5+
import java.io.IOException;
6+
import java.nio.charset.StandardCharsets;
7+
import java.nio.file.Files;
8+
import java.nio.file.Path;
9+
import java.nio.file.Paths;
10+
import java.nio.file.StandardOpenOption;
11+
import java.util.Arrays;
12+
import java.util.List;
13+
14+
import org.junit.jupiter.api.Test;
15+
16+
/**
17+
* @author niulang
18+
* @date 2023/11/06
19+
*/
20+
public class FileCreateAndWriteDemo {
21+
22+
@Test
23+
public void test0() throws IOException {
24+
String content = "www.wdbyte.com,java 7";
25+
String filePath = "/Users/darcy/wdbyte/test0.txt";
26+
try (FileWriter fw = new FileWriter(filePath);
27+
BufferedWriter bw = new BufferedWriter(fw)) {
28+
bw.write(content);
29+
bw.newLine();
30+
}
31+
32+
// 追加模式
33+
try (FileWriter fw = new FileWriter(filePath, true);
34+
BufferedWriter bw = new BufferedWriter(fw)) {
35+
bw.write(content);
36+
bw.newLine();
37+
}
38+
}
39+
40+
/**
41+
* JDK 7
42+
*
43+
* @throws IOException
44+
*/
45+
@Test
46+
public void test1() throws IOException {
47+
String content = "www.wdbyte.com, java 7";
48+
Path path = Paths.get("/Users/darcy/wdbyte/test1.txt");
49+
// string -> bytes
50+
Files.write(path, content.getBytes(StandardCharsets.UTF_8));
51+
}
52+
53+
/**
54+
* JDK 7 追加
55+
* @throws IOException
56+
*/
57+
@Test
58+
public void test2() throws IOException {
59+
String content = "www.wdbyte.com,java 7" + System.lineSeparator();
60+
Path path = Paths.get("/Users/darcy/wdbyte/test2.txt");
61+
Files.write(path, content.getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE,
62+
StandardOpenOption.APPEND);
63+
Files.write(path, content.getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE,
64+
StandardOpenOption.APPEND);
65+
}
66+
67+
/**
68+
* JDK 7 写入 List
69+
*
70+
* @throws IOException
71+
*/
72+
@Test
73+
public void test3() throws IOException {
74+
List<String> dataList = Arrays.asList("www", "wdbyte", "com", "java 7");
75+
Path path = Paths.get("/Users/darcy/wdbyte/test3.txt");
76+
Files.write(path, dataList);
77+
}
78+
79+
@Test
80+
public void test4() throws IOException {
81+
String content = "www.wdbyte.com,java 7";
82+
Path path = Paths.get("/Users/darcy/wdbyte/test4.txt");
83+
// 默认 utf_8
84+
try (BufferedWriter bw = Files.newBufferedWriter(path)) {
85+
bw.write(content);
86+
bw.newLine();
87+
}
88+
// 追加模式
89+
try (BufferedWriter bw = Files.newBufferedWriter(path,
90+
StandardOpenOption.CREATE, StandardOpenOption.APPEND)) {
91+
bw.write(content);
92+
bw.newLine();
93+
}
94+
}
95+
96+
/**
97+
* JDK 11
98+
*
99+
* @throws IOException
100+
*/
101+
@Test
102+
public void test5() throws IOException {
103+
Path path = Paths.get("/Users/darcy/wdbyte/test5.txt");
104+
Files.writeString(path, "www.wdbyte.com,java 11");
105+
}
106+
107+
108+
}

core-java-modules/pom.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,13 @@
3333
<module>core-java-os</module>
3434
</modules>
3535

36+
<dependencies>
37+
<dependency>
38+
<groupId>org.junit.jupiter</groupId>
39+
<artifactId>junit-jupiter</artifactId>
40+
<version>5.9.1</version>
41+
<!-- <scope>test</scope>-->
42+
</dependency>
43+
</dependencies>
44+
3645
</project>

0 commit comments

Comments
 (0)