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 ...
随机推荐
- C++课程设计2
PS:大一下学期C++课程设计 1.成绩管理系统 #include<stdio.h> #include<string> #include<iostream> #in ...
- 2017蓝桥杯省赛C/C++B(补题中)
标题:等差素数列 2,3,5,7,11,13,....是素数序列. 类似:7,37,67,97,127,157 这样完全由素数组成的等差数列,叫等差素数数列. 上边的数列公差为30,长度为6. 200 ...
- mac下出现xcrun: error导致git、svn无法使用的解决办法
现象:xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun ...
- 高精速撸project A*B
啊这个乘法我真的想不起来了 所以我跑去看了之前交的代码 那个时候的我真纯真啊,用cin cout都不关同步的 哪像现在,随手先写个inline void in(),能getchar()就不scanf, ...
- selenium webdriver使用click一直失效问题的几种解决方法
想要爬取动态网页,很莫名的click失效.被这个问题困扰了很久,基本上把网上提到的所有方法试遍了,最终终于有个方法成功了,在这里总结一下. 这是我想要点击的网页,初始时实在0.5km上,它的class ...
- 【Struts2的执行流程,这个博主写的很详细】
http://blog.csdn.net/wjw0130/article/details/46371847
- 剑指Offer_12_矩阵中的路径(参考问题:马踏棋盘)
题目描述 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径.路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子.如果一条路径经过了矩阵 ...
- ArrayList与数组间的转换
关键句:String[] array = (String[])list.toArray(new String[size]); public class Test { public static voi ...
- android代码混淆笔记
混淆处理的apk被反编译后代码中包名类名等都变成abcd之类.非常难看懂. 使用代码混淆.启用混淆器,对相关文件进行编辑,然后打包签名就能够了: ------------ 在2.3的版本号中,项目中有 ...
- 在dotnet core web api中支持CORS(跨域访问)
最近在写的Office add-in开发系列中,其中有一个比较共性的问题就是在add-in的客户端脚本中访问远程服务时,要特别注意跨域访问的问题. 关于CORS的一些基本知识,请参考维基百科的说明:h ...