如何解决java读取txt文件乱码问题

发布时间:2020-06-21 22:31:38 作者:鸽子
来源:亿速云 阅读:207

java读取txt文件,如果编码格式不匹配,就会出现乱码现象。所以读取txt文件的时候需要设置读取编码。txt文档编码格式都是写在文件头的,在程序中需要先解析文件的编码格式,获得编码格式后,在按此格式读取文件就不会产生乱码了。

java编码与txt编码对应:

如何解决java读取txt文件乱码问题

示例:

package com.lfl.attachment;  
  
import java.io.BufferedReader;  
import java.io.File;  
import java.io.FileInputStream;  
import java.io.InputStream;  
import java.io.InputStreamReader;  
  
public class TextMain {  
  
    public static void main(String[] args) throws Exception {  
        String filePath = "D:/article.txt";  
//      String filePath = "D:/article333.txt";    
//      String filePath = "D:/article111.txt";    
        String content = readTxt(filePath);  
        System.out.println(content);  
          
    }  
  
      
      
    /** 
     * 解析普通文本文件  流式文件 如txt 
     * @param path 
     * @return 
     */  
    @SuppressWarnings("unused")  
    public static String readTxt(String path){  
        StringBuilder content = new StringBuilder("");  
        try {  
            String code = resolveCode(path);  
            File file = new File(path);  
            InputStream is = new FileInputStream(file);  
            InputStreamReader isr = new InputStreamReader(is, code);  
            BufferedReader br = new BufferedReader(isr);  
//          char[] buf = new char[1024];  
//          int i = br.read(buf);  
//          String s= new String(buf);  
//          System.out.println(s);  
            String str = "";  
            while (null != (str = br.readLine())) {  
                content.append(str);  
            }  
            br.close();  
        } catch (Exception e) {  
            e.printStackTrace();  
            System.err.println("读取文件:" + path + "失败!");  
        }  
        return content.toString();  
    }  
      
      
      
    public static String resolveCode(String path) throws Exception {  
//      String filePath = "D:/article.txt"; //[-76, -85, -71]  ANSI  
//      String filePath = "D:/article111.txt";  //[-2, -1, 79] unicode big endian  
//      String filePath = "D:/article222.txt";  //[-1, -2, 32]  unicode  
//      String filePath = "D:/article333.txt";  //[-17, -69, -65] UTF-8  
        InputStream inputStream = new FileInputStream(path);    
        byte[] head = new byte[3];    
        inputStream.read(head);      
        String code = "gb2312";  //或GBK  
        if (head[0] == -1 && head[1] == -2 )    
            code = "UTF-16";    
        else if (head[0] == -2 && head[1] == -1 )    
            code = "Unicode";    
        else if(head[0]==-17 && head[1]==-69 && head[2] ==-65)    
            code = "UTF-8";    
            
        inputStream.close();  
          
        System.out.println(code);   
        return code;  
    }  
      
}

注意:在resolveTxt方法中不能通过readTxt方法传InputStream流 ,这样会使两个方法持有同一个流引用,而在resolveTxt方法中已读过流中的三个字节,流中的pos此时已经是3了,而不是流的起始位置,再在readTxt中读取时就会出现IOException:Read Error。

以上就是java读取txt文件乱码解决方法的详细内容,更多请关注亿速云其它相关文章!

推荐阅读:
  1. 如何解决java读取文件乱码问题
  2. java读取txt文件时出现中文乱码怎么解决

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java ava 读取txt文件

上一篇:java如何判断file文件是否存在

下一篇:Raid磁盘阵列真的是100%的安全吗?raid有哪些常见的故障?

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》