Aus irgendeinem Grund ändert sich jedes Mal, wenn sich ein Projekt ändert, auch die verwendete Programmiersprache. Jedes Mal google ich, wie die Textdatei eingegeben und ausgegeben wurde. Die aktuelle Seite ist Java, also lassen Sie es uns zusammenfassen!
final String baseDir = "/Users/yasuhiro/Documents/tmp";
File f = new File(baseDir + "/timestamp.txt");
StringBuffer sb = new StringBuffer();
String[] lineSeparator = { "" };
BufferedReader br = new BufferedReader(new FileReader(f));
br.lines().forEach(line -> {
sb.append(lineSeparator[0]);
sb.append(line);
lineSeparator[0] = System.lineSeparator();
});
br.close();
final String baseDir = "/Users/yasuhiro/Documents/tmp";
File f = new File(baseDir + "/timestamp.txt");
StringBuffer sb = new StringBuffer();
String[] lineSeparator = { "" };
try (BufferedReader br = new BufferedReader(new FileReader(f));) {
br.lines().forEach(line -> {
sb.append(lineSeparator[0]);
sb.append(line);
lineSeparator[0] = System.lineSeparator();
});
} catch (Exception e) {
e.printStackTrace();
}
final String baseDir = "/Users/yasuhiro/Documents/tmp";
File f = new File(baseDir + "/timestamp.txt");
BufferedWriter bw = new BufferedWriter(new FileWriter(f));
lines.forEach(line -> {
try {
bw.write(line);
bw.newLine();
} catch (IOException e) {
e.printStackTrace();
}
});
bw.close();
final String baseDir = "/Users/yasuhiro/Documents/tmp";
File f = new File(baseDir + "/timestamp.txt");
try (BufferedWriter bw = new BufferedWriter(new FileWriter(f));) {
lines.forEach(line -> {
try {
bw.write(line);
bw.newLine();
} catch (IOException e) {
e.printStackTrace();
}
});
} catch (IOException e) {
for (Throwable t : e.getSuppressed()) {
t.printStackTrace();
}
}
Recommended Posts