Java Converts disparate character codes to the same character code at once

Converts different character codes of Java files to the same character code at once and saves

    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

Java Converts disparate character codes to the same character code at once
Guess the character code in Java
How to call multiple names at once in the same category
I implemented the code to learn multiple images at once in the Watson Visual Recognition Collection in Java.
Java character code
Catch up on new features from Java 7 to Java 9 at once
[Java] To know the type information of type parameters at runtime
Correct the character code in Java and read from the URL
Input to the Java console
Setting to start multiple units at the same time with Vagrant
Determine if the strings to be compared are the same in Java
Sample code to call the Yahoo! Local Search API in Java
[java8] To understand the Stream API
Compile Java at the command prompt
Once you see the fucking code
Welcome to the Java Library Swamp! !!
The road from JavaScript to Java
[Android] Convert Android Java code to Kotlin
[Java: memorandum] Until the line feed code CRLF is changed to LF