java读取txt时出现乱码

ylgxngg 2010-05-26 09:58:56
就是一个很简单的小程序,要读入txt中的文本。程序如下:

package lesson;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;


public class c {
static String str;

static char[] charnum=new char[1024];
static int i=0;

public static void main(String[] args) {
File aFile = new File("D:/Java/example.txt");
FileInputStream inFile = null;

try {
inFile = new FileInputStream(aFile);

} catch(FileNotFoundException e) {
e.printStackTrace(System.err);
System.exit(1);
}

FileChannel inChannel = inFile.getChannel();
ByteBuffer buf = ByteBuffer.allocate(1024);
try {

while(inChannel.read(buf) != -1) {
str= ((ByteBuffer)(buf.flip())).asCharBuffer().toString();
System.out.println(str);
System.out.println(new String(str.getBytes(),"UTF-8"));
buf.clear();
}
inFile.close();
} catch(IOException e) {
e.printStackTrace(System.err);
System.exit(1);
}
}
}

现在的状况就是txt读取全部是乱码。。
“???????????獯潯潯潯潯潯潯潯潯漪??????????漪??????????漪??????????漪??????????漪??????????漪??????????漪??????????漪??????????漪??????????漪??????????漪?????????潯漪??????????漪??????????漪??????????漪??????????漪??????????漪??????????攪???????????”


我想问下大家怎么修改这个程序能让它正确读出文本?
谢谢!!!
...全文
326 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
武津乐 2010-05-28
  • 打赏
  • 举报
回复
windows的默认编码是GBK,你转成了UTF-8肯定是乱码了!
CrazyPastor 2010-05-28
  • 打赏
  • 举报
回复
JAVA和Window有一个编码.是什么忘了你最好找找
zitian666 2010-05-28
  • 打赏
  • 举报
回复
用BufferedReader类的对象去读
这个是按字符去读
hardycheng 2010-05-27
  • 打赏
  • 举报
回复
先看一下你的txt是什么编码的
john_kou 2010-05-27
  • 打赏
  • 举报
回复
呵呵,今天老是才给我们讲了。
欢乐极客 2010-05-26
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 daimojingdeyu 的回复:]

UTF-8的吗?读的时候需要注意文件存的时候有没有BOM编码,如果有要跳过。
另外下面的转码有点乱,转成CharBuffer时会有字符补齐的情况,导致再转成
byte[]时会和文件中的编码不一样。是否可以试着用ByteBuffer.array()直接转成byte[]
Java code

while(inChannel.read(buf) != -1) {
str= ((Byt……
[/Quote]

tx183584 2010-05-26
  • 打赏
  • 举报
回复
学习了..
daimojingdeyu 2010-05-26
  • 打赏
  • 举报
回复
UTF-8的吗?读的时候需要注意文件存的时候有没有BOM编码,如果有要跳过。
另外下面的转码有点乱,转成CharBuffer时会有字符补齐的情况,导致再转成
byte[]时会和文件中的编码不一样。是否可以试着用ByteBuffer.array()直接转成byte[]

while(inChannel.read(buf) != -1) {
str= ((ByteBuffer)(buf.flip())).asCharBuffer().toString();
System.out.println(str);
System.out.println(new String(str.getBytes(),"UTF-8"));


gesanri 2010-05-26
  • 打赏
  • 举报
回复

package lesson;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;


public class c {
static String str;

static char[] charnum=new char[1024];
static int i=0;

public static void main(String[] args) {
File aFile = new File("D:/Java/example.txt");
FileInputStream inFile = null;

try {
inFile = new FileInputStream(aFile);

} catch(FileNotFoundException e) {
e.printStackTrace(System.err);
System.exit(1);
}

FileChannel inChannel = inFile.getChannel();
ByteBuffer buf = ByteBuffer.allocate(1024);
try {

while(inChannel.read(buf) != -1) {
str= new String(buf.array(), "gbk");
System.out.println(str);
buf.clear();
}
inFile.close();
} catch(IOException e) {
e.printStackTrace(System.err);
System.exit(1);
}
}
}
cherie1215225 2010-05-26
  • 打赏
  • 举报
回复
顶!。。。。。。。。。。。。。。。。。。。。。
一个人单干 2010-05-26
  • 打赏
  • 举报
回复
FileInputStream是按字节来读的 但是汉字一次占两个字节,所以会有乱码,用FileReader来读,它是按字符读的。
铁匠梁老师 2010-05-26
  • 打赏
  • 举报
回复

System.out.println(new String(str.getBytes(),"UTF-8"));
//字符集编码问题,换成系统默认的试试
SealedBook 2010-05-26
  • 打赏
  • 举报
回复
编码问题。
不知道行不行。用文本编译器打开。之后改一下编码方式。改成utf-8再读试试。
我没试验。不知道行不行哈

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧