一、字符串追加写入文本
public static void fileWrite() {
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter("D://bbb.txt", true));
bw.write("你好");
bw.newLine();
bw.write("世界");
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
try {
bw.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
二、一行一行读取文本字符串
public static List<String> fileRead() {
BufferedReader bufferReader = null;
List<String> result = new ArrayList<>();
try {
bufferReader = new BufferedReader(new FileReader("D://bbb.txt"));
String tempString;
while ((tempString = bufferReader.readLine()) != null) {
result.add(tempString);
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
try {
bufferReader.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return result;
}