Convertissez simultanément différents codes de caractère de fichiers Java en le même code de caractère et enregistrez
public void writeFile() throws IOException {
Files.list(Paths.get("dir"))
.filter(p -> p.toFile().getName().endsWith(".txt"))
.forEach(p -> writeFile(p));
Files.find(Paths.get("dir")
, Integer.MAX_VALUE
, (p, a) -> p.toFile().getName().endsWith(".txt"))
.forEach(p -> writeFile(p));
}
public void writeFile(Path path) {
try {
Files.write(path
, readFile(path).stream().collect(Collectors.joining("\r")).getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
throw new IllegalStateException("write error file : " + path.toAbsolutePath(), e);
}
}
public List<String> readFile(Path path) {
for (Charset charset: Charset.availableCharsets().values()) {
try {
return Files.readAllLines(path, charset);
} catch (MalformedInputException e) {
e.printStackTrace();
continue;
} catch (IOException e) {
throw new IllegalStateException("read error file : " + path.toAbsolutePath(), e);
}
}
throw new IllegalStateException("read error file : " + path.toAbsolutePath());
}
Recommended Posts