这是我写的一个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文件中中文字符时,出现乱码的解决办法的更多相关文章

  1. java 中读取本地文件中字符

    java读取txt文件内容.可以作如下理解: 首先获得一个文件句柄.File file = new File(); file即为文件句柄.两人之间连通电话网络了.接下来可以开始打电话了. 通过这条线路 ...

  2. Flex读取txt文件中的内容(三)

    Flex读取txt文件中的内容 1.设计源码 LoadTxt.mxml: <?xml version="1.0" encoding="utf-8"?> ...

  3. Flex读取txt文件中的内容(二)

    Flex读取txt文件中的内容 自动生成的文件 LoadTxt-app.xml: <?xml version="1.0" encoding="utf-8" ...

  4. Flex读取txt文件中的内容(一)

    Flex读取txt文件中的内容 phone.txt: 13000003847 13000003848 13000003849 13000003850 13000003851 13000003852 1 ...

  5. Flex读取txt文件中的内容报错

    Flex读取txt文件中的内容 1.具体错误如下 2.错误原因 读取文件不存在 var file:File = new File(File.applicationDirectory.nativePat ...

  6. 按行读取TXT文件中的内容

    public Dictionary<int, string> GetDicFromLog() { try { StreamReader sr = new StreamReader(file ...

  7. Python笔记(读取txt文件中的数据)

    在机器学习中,常常需要读取txt文本中的数据,这里主要整理了两种读取数据的方式 数据内容 共有四列数据,前三列为特征值,最后一列为数据标签 40920 8.326976 0.953952 3 1448 ...

  8. 新手C#s.Split(),s.Substring(,)以及读取txt文件中的字符串的学习2018.08.05

    s.split()用于字符串分割,具有多种重载方法,可以通过指定字符或字符串分割原字符串成为字符串数组. //s.Split()用于分割字符串为字符串数组,StringSplitOptions.Rem ...

  9. Newtonsoft.Json读取txt文件中json数据并存到SQL service 数据库!

    using System; using System.Collections.Generic; using System.Text; using System.IO; using Newtonsoft ...

随机推荐

  1. TCP/IP协议栈 --- 网络层(IP 首部 和分片)

    IP 是TCP/IP协议栈中重要的层次, TCP UDP ICMP IGMP都是依赖IP层进行传输的.首先它是一种不可靠,无连接的协议.不可靠:它不保证IP包能正确到达目的地,无连接:表示IP并不会维 ...

  2. 如果Centos没有桌面,怎么修改IP地址

    修改前备份!!! 修改前备份!!! 修改前备份!!! [PHP] 纯文本查看 复制代码 ? 1 vi /etc/sysconfig/network-scripts/ifcfg-eth0 编辑,输入以下 ...

  3. Redis在centos6.4上的最详细图文安装教程

    准备工作:一个redis3.0.0的安装包,没有可以点击下面的链接下载 https://pan.baidu.com/s/1kU5Ez2J 工具 安装环境   centos6.4 好了开始进入正题 输入 ...

  4. 两个实用linux小工具

    使用 sshpass 工具来做名密码输入 使用 alias 别名来做成命令语句. Linux命令之非交互SSH密码验证-sshpass ssh登陆不能在命令行中指定密码.sshpass的出现,解决了这 ...

  5. jquery on()动态绑定元素的的点击事件无反应的问题记录

    1.jquery使用版本:v2.0 2.重现代码: html <table class="table"> <thead> <tr> <th ...

  6. backbone与require的共存问题解决

    如果向下面那样直接列出script标签可能会出现错误,   <script type="text/javascript" src="/dep/jquery-1.11 ...

  7. 基于跳跃表的 ConcurrentSkipListMap 内部实现(Java 8)

    我们知道 HashMap 是一种键值对形式的数据存储容器,但是它有一个缺点是,元素内部无序.由于它内部根据键的 hash 值取模表容量来得到元素的存储位置,所以整体上说 HashMap 是无序的一种容 ...

  8. [Shell]crontab 运行任务调用shell脚本,相对路径无法找到

    问题出现的场景大概就是 1  cron调用一个python脚本 2  python脚本中调用一个shell脚本(对日志分析)获取shell输出然后发送邮件 类似一个监控任务. 直接运行python脚本 ...

  9. 启用oracle 11g自己主动收集统计信息

    今天接到朋友数据库一个case,在DBCA建库时,把自己主动收集统计信息的选项去掉了,数据库执行半年没有收集过统计信息.如今要启用方法例如以下: exec DBMS_AUTO_TASK_ADMIN.E ...

  10. 大白话Vue源码系列(03):生成render函数

    阅读目录 优化 AST 生成 render 函数 小结 本来以为 Vue 的编译器模块比较好欺负,结果发现并没有那么简单.每一种语法指令都要考虑到,处理起来相当复杂.上篇已经生成了 AST,本篇依然对 ...