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 ...
随机推荐
- ##3.Keystone 验证服务--openstack
##3.Keystone 验证服务 openstack pike 安装 目录汇总 http://www.cnblogs.com/elvi/p/7613861.html #SQL上创建数据库并授权 #K ...
- IntelliJ IDEA(二) :面板介绍
一.面板说明 IDEA面板的全貌如下图 二.菜单栏 下面会简单介绍下一些常用的部分菜单使用,如有疑问或补充欢迎留言. (1).File文件 1. New:新建一个工程 可以新建project,导入已存 ...
- 蓝桥杯 牌型种数 DFS
牌型种数 小明被劫持到X赌城,被迫与其他3人玩牌. 一副扑克牌(去掉大小王牌,共52张),均匀发给4个人,每个人13张. 这时,小明脑子里突然冒出一个问题: 如果不考虑花色,只考虑点数,也不考虑自己得 ...
- 鼠标悬停,图片放大 CSS实现
因为最近做的项目刚好用到了这个实现,分享出来 class=enlarge 为div标签的class div img 为标签 .enlarge div img:hover{ transform: s ...
- 我的第一个python web开发框架(18)——前台页面与接口整合
由于我们前后台系统没有分开,所以前台页面调用接口时,可以直接使用后台管理系统已经完成的接口,不过后台管理系统接口的访问加上了登录验证,所以需要将前台要用到的接口进行处理,让它们设置到白名单当中 我们打 ...
- C#中级-从零打造基于Socket在线升级模块
一.前言 前段时间一直在折腾基于Socket的产品在线升级模块.之前我曾写过基于.Net Remoting的.基于WCF的在线升级功能,由于并发量较小及当时代码经验的不足一直没有实际应用. ...
- 》》jquery-weui 初
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta na ...
- EventBus猜想 ----手把手带你自己实现一个EventBus
本文是什么 本文是一篇怀着推測角度学习一个未知东西(EventBus)的文章. 先推測EventBus是怎样实现的. 依据推測去模仿他的实现. 查看源代码.验证猜想. 更深入的去理解他. 转载请注明出 ...
- 在linux上手动搭建svn服务器
svn服务器的搭建 环境: linux CentOS 7 安装: 1.安装svn服务器 yum install subversion 2.查看版本 svnserve --version 3.创建版本库 ...
- 尝试造了个工具类库,名为 Diana
项目地址: diana 文档地址: http://muyunyun.cn/diana/ 造轮子的意义 为啥已经有如此多的前端工具类库还要自己造轮子呢?个人认为有以下几个观点吧: 定制性强,能根据自己的 ...