Skip to content

Commit 189f2b5

Browse files
committed
append file
1 parent dd0e71c commit 189f2b5

1 file changed

Lines changed: 124 additions & 0 deletions

File tree

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package com.mkyong.io.file;
2+
3+
import org.apache.commons.io.FileUtils;
4+
5+
import java.io.*;
6+
import java.nio.charset.StandardCharsets;
7+
import java.nio.file.Files;
8+
import java.nio.file.Path;
9+
import java.nio.file.StandardOpenOption;
10+
import java.util.List;
11+
12+
// https://mkyong.com/java/how-to-append-content-to-file-in-java/
13+
public class FileAppend {
14+
15+
private static final String NEW_LINE = System.lineSeparator();
16+
17+
public static void main(String[] args) throws IOException {
18+
19+
//Path path = Paths.get("/home/mkyong/test/abc.txt");
20+
appendToFile(path, "hello world" + NEW_LINE);
21+
22+
/*File file = new File("/home/mkyong/test/abc.txt");
23+
appendToFileFileWriter(file, "hello world" + NEW_LINE);
24+
System.out.println("Done");*/
25+
26+
}
27+
28+
// Java 7
29+
private static void appendToFile(Path path, String content) throws IOException {
30+
31+
// if file not exists throws java.nio.file.NoSuchFileException
32+
// Files.write(path, content.getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
33+
34+
// if file not exists, create it otherwise append to the end of the file
35+
Files.write(path, content.getBytes(StandardCharsets.UTF_8),
36+
StandardOpenOption.CREATE,
37+
StandardOpenOption.APPEND);
38+
39+
}
40+
41+
42+
// append lines of text
43+
private static void appendToFileJava8(Path path, List<String> list) throws IOException {
44+
45+
// Java 7?
46+
// Files.write(path, list, StandardCharsets.UTF_8,
47+
// StandardOpenOption.CREATE,
48+
// StandardOpenOption.APPEND);
49+
50+
// Java 8, default utf_8
51+
Files.write(path, list,
52+
StandardOpenOption.CREATE,
53+
StandardOpenOption.APPEND);
54+
55+
}
56+
57+
// Java 11, writeString, append mode
58+
private static void appendToFileJava11(Path path, String content) throws IOException {
59+
60+
// utf_8
61+
// Files.writeString(path, content, StandardCharsets.UTF_8,
62+
// StandardOpenOption.CREATE,
63+
// StandardOpenOption.APPEND);
64+
65+
// default StandardCharsets.UTF_8
66+
Files.writeString(path, content,
67+
StandardOpenOption.CREATE,
68+
StandardOpenOption.APPEND);
69+
}
70+
71+
private static void appendToFileFileWriter(File file, String content) throws IOException {
72+
73+
// default - create and write
74+
// if file not exists, create and write
75+
// if file exists, truncate and write
76+
/*try (FileWriter fw = new FileWriter(file);
77+
BufferedWriter bw = new BufferedWriter(fw)) {
78+
bw.write(content);
79+
bw.newLine();
80+
}*/
81+
82+
// append
83+
// if file not exists, create and write
84+
// if file is exists, append to the end of the file
85+
try (FileWriter fw = new FileWriter(file, true);
86+
BufferedWriter bw = new BufferedWriter(fw)) {
87+
bw.write(content);
88+
bw.newLine(); // add new line, System.lineSeparator()
89+
}
90+
91+
}
92+
93+
private static void appendToFileFileWriter(File file, List<String> content) throws IOException {
94+
95+
try (FileWriter fw = new FileWriter(file, true);
96+
BufferedWriter bw = new BufferedWriter(fw)) {
97+
98+
for (String s : content) {
99+
bw.write(s);
100+
bw.newLine();
101+
}
102+
}
103+
104+
}
105+
106+
private static void appendToFileFileOutputStream(File file, String content) throws IOException {
107+
108+
// append mode
109+
try (FileOutputStream fos = new FileOutputStream(file, true)) {
110+
fos.write(content.getBytes(StandardCharsets.UTF_8));
111+
}
112+
113+
}
114+
115+
// append mode
116+
private static void appendToFileFileUtils(File file, String content) throws IOException {
117+
118+
FileUtils.writeStringToFile(
119+
file, content, StandardCharsets.UTF_8, true);
120+
121+
}
122+
123+
124+
}

0 commit comments

Comments
 (0)