|
| 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 | +} |
0 commit comments