Java读取TXT文本乱码解决方案

上传人:公**** 文档编号:563739175 上传时间:2022-11-14 格式:DOC 页数:4 大小:26KB
返回 下载 相关 举报
Java读取TXT文本乱码解决方案_第1页
第1页 / 共4页
Java读取TXT文本乱码解决方案_第2页
第2页 / 共4页
Java读取TXT文本乱码解决方案_第3页
第3页 / 共4页
Java读取TXT文本乱码解决方案_第4页
第4页 / 共4页
亲,该文档总共4页,全部预览完了,如果喜欢就下载吧!
资源描述

《Java读取TXT文本乱码解决方案》由会员分享,可在线阅读,更多相关《Java读取TXT文本乱码解决方案(4页珍藏版)》请在金锄头文库上搜索。

1、Java读取TXT文本文件乱码解决方案今天在解析一个文本文件的时候出现了乱码,以前从未遇到,花了点时间解决了,在此总结一下:首先,先看一下解析的代码:private String getProjectData()String data = null;try BufferedReader br = new BufferedReader(new (file);String str = br.readLine();data = str.split(t);br.close(); catch ( e) e.printStackTrace(); catch (IOException e) e.printS

2、tackTrace();return data;这个方法很简单,就是获取txt文件中的一行数据,然后转换为数组,我这个文本中只有一行。就这种情况下获取到的str是乱码,我想出现乱码肯定是字符集编码的问题,然后自己新建了一个文本文档测试没有问题(开始用的文本时别的程序导出的),所以我估计是开始那个txt文件编码的问题,查看发现是Unicode编码(如何查看txt的编码呢,教你一个笨笨的办法:打开文件另存为,然后看到最下面的编码,默认选择的就是当前文档的编码格式),问题就出现在这里。问题找到了就好解决了,修改代码为:private String getProjectData()String dat

3、a = null;try BufferedReader br = new BufferedReader(new InputStreamReader(new (file),UTF-16);String str = br.readLine();data = str.split(t);br.close(); catch ( e) e.printStackTrace(); catch (IOException e) e.printStackTrace();return data;使用文件流读取,转换编码为“UTF-16”,这样乱码问题就解决了。归纳一下:txt文本文档有四种编码选项:ANSI、Unic

4、ode、Unicode big endian、UTF-8;默认应该是ANSI选项,就是系统的默认编码,一般是GBK,这种情况下用第一段代码解析是没有问题的,也不需要转码;其他格式就需要用第二段代码对应转码了,Unicode对应UTF-16,UTF-8就不用说了。关于获取txt文件编码,我们可能有时候需要用程序获取,动态判断,这里给一点资料,参考参考:ANSI: 无格式定义Unicode: 前两个字节为FFFE Unicode文档以0xFFFE开头Unicode big endian: 前两字节为FEFFUTF-8: 前两字节为EFBB UTF-8以0xEFBBBF开头用程序取出前几个字节并进行

5、判断即可。package com.syscan.oa.util;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.;import java.io.;import java.io.;import java.io.IOException;import java.util.ArrayList;import java.util.List;public class ReadWritTxtFileIO public static void main(String ar

6、gs) /ReadDate();/WriteDate();String path = d:/;String str = writeToDat(path);System.out.println(str.length = +str.length);for (int i = 0; i str.length; i+) System.out.println(stri);String splitStr = stri.split(#);System.out.println(splitStr.length = +splitStr.length);for(int j = 0; j splitStr.length

7、; j+)System.out.println(splitStrj);/* * 读取数据 */public static void ReadDate() String url = e:/2.txt;try read = new (new );StringBuffer sb = new StringBuffer();char ch = new char1024;int d = read.read(ch);while (d != -1) String str = new String(ch, 0, d);sb.append(str);d = read.read(ch);System.out.pri

8、nt(sb.toString(); catch ( e) e.printStackTrace(); catch (IOException e) e.printStackTrace();/* * 写入数据 */SuppressWarnings(unchecked)public static void WriteDate() try = new File(D:/abc.txt);if () ();();BufferedWriter output = new BufferedWriter(new (file);ArrayList ResolveList = new ArrayList();for (

9、int i = 0; i 10; i+) ResolveList.add(Math.random() * 100);for (int i = 0; i ResolveList.size(); i+) output.write(String.valueOf(ResolveList.get(i) + n);output.close(); catch (Exception ex) System.out.println(ex);SuppressWarnings(unchecked)public static String writeToDat(String path) = new );List lis

10、t = new ArrayList();String str = null;try BufferedReader bw = new BufferedReader(new (file);String line = null;/ 因为不知道有几行数据,所以先存入list集合中while (line = bw.readLine() != null) list.add(line);bw.close(); catch (IOException e) e.printStackTrace();/ 确定数组长度str = new Stringlist.size();for (int i = 0; i list.size(); i+) String s = (String) list.get(i);stri = s;return str; / 文档可自由编辑打印

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 解决方案

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号