您的位置 首页 技术

java读取txt文件乱码解决方法

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

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

java编码与txt编码对应:

1.jpg

示例:

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知识请关注java基础教程栏目。

以上就是java读取txt文件乱码解决方法的详细内容,更多请关注24课堂在线网其它相关文章!

本文来自网络,不代表24小时课堂在线立场,转载请注明出处:https://www.24ketang.cn/10668.html

为您推荐

返回顶部