Total Pageviews

2020/07/09

[Java] How to read file with encoding Cp937?

Problem
How to read file with encoding Cp937?

How-To
Here has code snippet:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;

....

    pubilc void readAndPrintContent(String file) throws IOException {
        try (Stream<String> stream = Files.lines(Paths.get(file), Charset.forName("Cp937"))) {
            stream.forEach(log::debug);
        } catch (IOException e) {
            throw new IOException("fail to read file, error : " + e.getMessage(), e);
        }
    }



No comments: