读取含有中文的文本文件出现乱码?(急) 试了很多方法读出来的都是乱码,郁闷…… #$#%请教下大家是如何处理这类问题的?最好能贴下具体的代码 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用ultraedit把字符集转一下好像就可以了,读出来不会有问题的 你文本文件的编码可能就不对如果用utf-8编码,程序直接读就行了 看看这http://community.csdn.net/Expert/topic/4496/4496606.xml?temp=.9900629 FileInputStream 按字节读取文件.而中文是unicode的话,就是一个汉字字符两个字节.所以用FileReader就可以按unicode来读取字符.因此文本也最好保存为unicode.读取中文字符最好用FileReader类 这是个典型的中文编码的问题你的 txt 文件中的一般是 gb2312的编码试着转换编码就行了如: String str;Byte[] b = str.getBytes(txt,"utf-8"); 给你一个源码FileReader.书上的,研究一下嘛.向文件写的是密文,你也可以把他改成明文import java.io.*; import java.awt.*; import java.awt.event.*;class fileread{public static void main(String args[]){ char a[]="今晚10点发起总攻".toCharArray(); int n=0,m=0; try { File f = new File("c:\\","text.txt"); for (int i=0;i<a.length;i++) {a[i]=(char)(a[i]^'R');} FileWriter out=new FileWriter(f); out.write(a,0,a.length); out.close(); FileReader in=new FileReader(f); int length=(int)f.length(); char tom[]=new char[length]; while ((n=in.read(tom,0,length))!=-1) { String s=new String(tom,0,n); m=n; System.out.println("密文:"+s); } in.close(); for (int i=0;i<m;i++) {tom[i]=(char)(tom[i]^'R');} String 明文=new String(tom,0,m); System.out.println("明文:"+明文); } catch (IOException e) { System.out.println("File read error"+e);}}} 用FileReader可以正常显示,但在显示完所有文本内容后不知为何又多了许多 “□□□”符号 NetBeans 使用 方法?? 怎样用它写程序??求解 1.5转1.4的泛型问题 如何遍历可执行jar包中的目录内容?? 求助:java.lang.NoClassRefFoundError StringBuffer问题 java在windows和sorlaris下的不同表现~~,帮忙看看 String是引用数据类型 但是是值传递 怎么理解? 如何加载包 还是关于消息框的问题 applet中的问题 HELP ME!!! 守护进程总是不起作用? 求助:字符串中出现次数最多的单词及其出现次数
如果用utf-8编码,程序直接读就行了
http://community.csdn.net/Expert/topic/4496/4496606.xml?temp=.9900629
而中文是unicode的话,就是一个汉字字符两个字节.
所以用FileReader就可以按unicode来读取字符.因此
文本也最好保存为unicode.
读取中文字符最好用FileReader类
你的 txt 文件中的一般是 gb2312的编码
试着转换编码就行了
如: String str;
Byte[] b = str.getBytes(txt,"utf-8");
class fileread{
public static void main(String args[]){
char a[]="今晚10点发起总攻".toCharArray();
int n=0,m=0;
try {
File f = new File("c:\\","text.txt");
for (int i=0;i<a.length;i++)
{a[i]=(char)(a[i]^'R');}
FileWriter out=new FileWriter(f);
out.write(a,0,a.length);
out.close();
FileReader in=new FileReader(f);
int length=(int)f.length();
char tom[]=new char[length];
while ((n=in.read(tom,0,length))!=-1)
{
String s=new String(tom,0,n);
m=n;
System.out.println("密文:"+s);
}
in.close();
for (int i=0;i<m;i++)
{tom[i]=(char)(tom[i]^'R');}
String 明文=new String(tom,0,m);
System.out.println("明文:"+明文);
}
catch (IOException e)
{ System.out.println("File read error"+e);}
}
}