Java中读取txt文件中中文字符时,出现乱码的解决办法
这是我写的一个Java课程作业时,遇到的问题。
问题描述:
我要实现的就是将txt文件中的内容按一定格式读取出来后,存放在相应的数组。
我刚开始运行时发现,英文可以实现,但是中文字符就是各种乱码。
最后各种检查排错得到原因如下:
(1)最好先检查你的Eclipse中的Window->Preference->General->WorkSpace中的字符编码格式是什么,最后选成UTF-8格式的。
然后右键你的工程在弹出的菜单项中选择Resource查看编码格式,也把它改成UTF-8格式的。
(2)再将InputStreamReader类对象 in2reader 中使用System.out.println(in2reader.getEncoding());
来查看编码格式。而且使用读取txt文件中的内容最好是按照字符流进行读取使用BufferReader,它里面有很方便的按行读取的函数readLine(),返回的是换行符前的字符串;
(3)使用Notepad++来对你的txt文件进行文件编码格式的选择,在菜单栏下的格式选项中。注意一定要选成UTF-8无BOM格式编码。
(4)运行你的程序就正确输出啦!
总结:其实这个问题最本质的解决办法的就是要把握好,文件本身格式的编码格式以及Java程序读取时的格式,把握好这一点,这个小问题就可以轻而易举地解决啦。
下面是我的参考示例代码:
/*
*从txt文件中读取数据存放到学生类数组中
*/ package woshijpf; import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader; public class Read_Stu_Info { private int stu_num = 0;
private File r_file,w_file;
private BufferedInputStream bin_r;
private BufferedOutputStream bout_w;
private InputStreamReader in2reader;
private BufferedReader br;
private Student[] stu_arr = null;
private String[] stuStrings= new String[3];
private int count = 0;
private String numString;
private Integer numInteger; public Read_Stu_Info(String r_filename,String w_filename)
{
r_file = new File(r_filename);
if(r_file.exists()==false)
{
try {
r_file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} w_file = new File(w_filename);
if(w_file.exists()==false)
{
try {
r_file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} try {
bin_r = new BufferedInputStream(new FileInputStream(r_file));
in2reader = new InputStreamReader(bin_r);
br = new BufferedReader(in2reader); //System.out.println(in2reader.getEncoding()); bout_w = new BufferedOutputStream(new FileOutputStream(w_file));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} try {
numString = br.readLine();
numInteger = Integer.valueOf(numString);
stu_num = numInteger.intValue();
System.out.println("学生人数: "+stu_num);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} stu_arr = new Student[stu_num]; } public int Getnum()
{
return stu_num;
} public void GetInfo()
{
String temp = null;
try {
while((temp = br.readLine())!=null)
{
stuStrings = temp.split(" ");
stu_arr[count++] = new Student(stuStrings[0], stuStrings[1], stuStrings[2]);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } public Student[] GetArray()
{
return stu_arr;
} public int GetStu_num()
{
return count;
} }
/*
*学生类
*/
package woshijpf; public class Student { private String name;
private String stunum;
private String score; public Student(String name,String stunum,String score)
{
this.name = name;
this.stunum = stunum;
this.score = score;
} public String getName()
{
return name;
} public String getNum()
{
return stunum;
} public String getScore()
{
return score;
} public void Print()
{
System.out.println("学生信息: ");
System.out.println("姓名: "+name+" 学号: "+stunum+" 分数: "+score);
} }
/*
*测试主函数类
*/
package woshijpf; public class Test_4 { public static void main(String[] args)
{
/*
* 题目五程序
*/
Student[] stu_array = null;
int count; Read_Stu_Info rsi = new Read_Stu_Info("a.txt", "a.dat");
rsi.GetInfo();
stu_array = rsi.GetArray();
count = rsi.GetStu_num();
for(int i=0;i<count;i++)
{
stu_array[i].Print();
} } }
Java中读取txt文件中中文字符时,出现乱码的解决办法的更多相关文章
- java 中读取本地文件中字符
java读取txt文件内容.可以作如下理解: 首先获得一个文件句柄.File file = new File(); file即为文件句柄.两人之间连通电话网络了.接下来可以开始打电话了. 通过这条线路 ...
- Flex读取txt文件中的内容(三)
Flex读取txt文件中的内容 1.设计源码 LoadTxt.mxml: <?xml version="1.0" encoding="utf-8"?> ...
- Flex读取txt文件中的内容(二)
Flex读取txt文件中的内容 自动生成的文件 LoadTxt-app.xml: <?xml version="1.0" encoding="utf-8" ...
- Flex读取txt文件中的内容(一)
Flex读取txt文件中的内容 phone.txt: 13000003847 13000003848 13000003849 13000003850 13000003851 13000003852 1 ...
- Flex读取txt文件中的内容报错
Flex读取txt文件中的内容 1.具体错误如下 2.错误原因 读取文件不存在 var file:File = new File(File.applicationDirectory.nativePat ...
- 按行读取TXT文件中的内容
public Dictionary<int, string> GetDicFromLog() { try { StreamReader sr = new StreamReader(file ...
- Python笔记(读取txt文件中的数据)
在机器学习中,常常需要读取txt文本中的数据,这里主要整理了两种读取数据的方式 数据内容 共有四列数据,前三列为特征值,最后一列为数据标签 40920 8.326976 0.953952 3 1448 ...
- 新手C#s.Split(),s.Substring(,)以及读取txt文件中的字符串的学习2018.08.05
s.split()用于字符串分割,具有多种重载方法,可以通过指定字符或字符串分割原字符串成为字符串数组. //s.Split()用于分割字符串为字符串数组,StringSplitOptions.Rem ...
- Newtonsoft.Json读取txt文件中json数据并存到SQL service 数据库!
using System; using System.Collections.Generic; using System.Text; using System.IO; using Newtonsoft ...
随机推荐
- bundles.Add( )下无法绑定后缀为min.css的文件
1.问题描述: 在绑定css的时候,除了后缀名为.min.css的文件,在render.style()不显示外,其他的css都正常加载, 2.解决办法: 这个是我在调试了几遍之后发现的规律,然后解决办 ...
- eclipse中将本地项目上传到svn库
转载文章:http://blog.csdn.net/singit/article/details/48972197
- TL-WR703Nv1.7刷写openwrt固件
TP-LINK TL-WR703N是一个小型的路由器,可以有线转WiFi,3G转WiFi,很多人拿它刷openwrt系统,然后可以在上面各种搞事. V1.7以前 通常刷openwrt的做法是, 下载一 ...
- CS Round#49 C Max Substring
Max Substring Time limit: 1000 msMemory limit: 256 MB You are given a string S. Find a string T th ...
- 使用背景图修改radio、checkbox样式
如果觉得设置样式太麻烦,或者页面上选中的样式太复杂,也可以用背景图去修改样式<div class=""> <label><input type=&qu ...
- OJ随笔——【1102-海明距离】——位运算
题目如下: Description 海明距离是在指二进制情况下,一个整数变成另外一个整数需要翻转的位数.比如2转换到3需要翻转1位,所以2到3的海明距离是1.给你两个正整数x和y,(x,y<=1 ...
- 自学Zabbix1.3-zabbix进程
默认情况下zabbix包含5个程序:zabbix_agentd.zabbix_get.zabbix_proxy.zabbix_sender.zabbix_server,另外一个zabbix_java_ ...
- JavaScript 数组最大值
JavaScript 数组最大值 在js中可以使用Math.max()获取最大值. 如: console.log(Math.max("1","11"," ...
- web前端学习就这9个阶段,你属于哪个阶段?
第一阶段:HTML+CSS: HTML进阶.CSS进阶.div+css布局.HTML+css整站开发. JavaScript基础:Js基础教程.js内置对象常用方法.常见DOM树操作大全.ECMAsc ...
- 微信小程序——轮播图实现
小程序的轮播图,也就是他其中的一个控件可以算是直接上代码: 这是WXML 页面 代码: <view class='carousel_div'> <swiper class=" ...