




全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Java读取TXT文本文件乱码解决方案今天在解析一个文本文件的时候出现了乱码,以前从未遇到,花了点时间解决了,在此总结一下:首先,先看一下解析的代码:private String getProjectData(File file)String data = null;try BufferedReader br = new BufferedReader(new FileReader(file);String str = br.readLine();data = str.split(t);br.close(); catch (FileNotFoundException e) e.printStackTrace(); catch (IOException e) e.printStackTrace();return data;这个方法很简单,就是获取txt文件中的一行数据,然后转换为数组,我这个文本中只有一行。就这种情况下获取到的str是乱码,我想出现乱码肯定是字符集编码的问题,然后自己新建了一个文本文档测试没有问题(开始用的文本时别的程序导出的),所以我估计是开始那个txt文件编码的问题,查看发现是Unicode编码(如何查看txt的编码呢,教你一个笨笨的办法:打开文件另存为,然后看到最下面的编码,默认选择的就是当前文档的编码格式),问题就出现在这里。问题找到了就好解决了,修改代码为:private String getProjectData(File file)String data = null;try BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file),UTF-16);String str = br.readLine();data = str.split(t);br.close(); catch (FileNotFoundException e) e.printStackTrace(); catch (IOException e) e.printStackTrace();return data;使用文件流读取,转换编码为“UTF-16”,这样乱码问题就解决了。归纳一下:txt文本文档有四种编码选项:ANSI、Unicode、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开头用程序取出前几个字节并进行判断即可。package com.syscan.oa.util;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.util.ArrayList;import java.util.List;public class ReadWritTxtFileIO public static void main(String args) /ReadDate();/WriteDate();String path = d:/file4.txt;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; j+)System.out.println(splitStrj);/* * 读取数据 */public static void ReadDate() String url = e:/2.txt;try FileReader read = new FileReader(new File(url);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.print(sb.toString(); catch (FileNotFoundException e) e.printStackTrace(); catch (IOException e) e.printStackTrace();/* * 写入数据 */SuppressWarnings(unchecked)public static void WriteDate() try File file = new File(D:/abc.txt);if (file.exists() file.delete();file.createNewFile();BufferedWriter output = new BufferedWriter(new FileWriter(file);ArrayList ResolveList = new ArrayList();for (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) File file = new File(path);List list = new ArrayList();String str = null;try BufferedReader bw = new BufferedReader(new FileReader(file);String line = null;/ 因为不知道有几行数据,所以先存入list集合中while (line = bw.readLine() != null) list.add(line);bw.close()
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 文化创意产业园施工总承包与配套设施建设合同
- 离婚后共同财产分割及房产分配执行协议
- 网络直播平台签约主播才艺展示合作合同范本
- 多源数据融合预测方法-第1篇-洞察及研究
- 教育机构消防安全合规性评估及2025年整改实施报告
- 场地租赁合同经典版本2篇
- 智能家居交互界面适应性研究-洞察及研究
- 仓储协议(紫铜浮雕)6篇
- 农业生态系统服务的空间异质性与社会经济影响-洞察及研究
- 中秋知识竞赛试题及答案
- 辽宁省名校联盟2025年高三9月份联合考试 物理试卷(含答案解析)
- 新产程解读及产程管理
- 2025高职单招职业适应性测试题库与答案
- 2025至2030中国摩托车保险行业调研及市场前景预测评估报告
- Unit 1 How can I get there?(教学设计)-2024-2025学年人教PEP版英语六年级上册
- 中学生物学教学技能与实践课件
- 井喷失控事故案例教育-井筒工程处课件
- 《农产品质量安全》系列讲座(第一讲-农产品质量及安全)课件
- 日语教程单词表(任卫平版)
- 托业考试Toeic考题
- IPM原理及测试方法ppt课件
评论
0/150
提交评论