For some reason, every time a project changes, so does the programming language used. Every time, I google how the text file was input and output. The current site is Java, so let's summarize it!
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