java ansi文件转utf8字符

windows上新建的txt, 用记事本编辑后保存就是ansi格式,也是gbk,java读取后乱码,下面是程序中转utf8

try {
    
    
    Path filePath = Paths.get(fileName);
    Files.lines(filePath, Charset.forName("CP1252")).forEach(s->{
    
    
        try {
    
    
            s =new String(s.getBytes("CP1252"), "GBK");
        } catch (UnsupportedEncodingException e) {
    
    
            e.printStackTrace();
        }
    });
} catch (IOException e) {
    
    
    log.error("error file:{}",fileName);
    e.printStackTrace();
}
//其他相同效果编码
new String(s.getBytes("CP1252"), "GBK");
new String(s.getBytes("Windows-1252"), "GBK");
new String(s.getBytes("ISO-8859-1"), "GBK");

java8-Supported Encodings

猜你喜欢

转载自blog.csdn.net/c5113620/article/details/104551302