java读取文件,读出问号了,该怎么办啊,

yifuzhiming 2007-12-11 03:28:01

以下程序可以执行,读取一个html文件,大部分的字符和中文都正常,有点问号,这是不正常显示,

public static void main(String[] args) {
String path="E:/dialog";
File list=new File(path);
for(int i=0;i<list.list().length;i++){
System.out.print(list.list()[i]);
String filepath=path+"/"+list.list()[i];
String content=replaceGb2312.readcontent(filepath);
System.out.print(content);
}
}

public static String readcontent(String path){
String str=new String();
try {
FileInputStream read=new FileInputStream(path);
byte [] body=new byte[read.available()];
int i=0;

while((i=read.read(body))!=-1){
str=str+new String(body);
}
read.close();
read=null;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return str.toString();
}
...全文
403 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
fly的狐狸 2011-03-30
  • 打赏
  • 举报
回复
我也出现了同样问题~求解答
DirectRay 2007-12-11
  • 打赏
  • 举报
回复
个人建议,看看是不是要处理一下Unicode的问题?你毕竟是开字节流在读,很有可能会抓个特殊编码的东西回来。比如说,\u000A.
gwu0318 2007-12-11
  • 打赏
  • 举报
回复
while((i=read.read(body))!=-1){
str=str+new String(body);
}

这里的改为new String("gb2312", i);
newwalk 2007-12-11
  • 打赏
  • 举报
回复
另提醒一下建议使用bufferedreader
yifuzhiming 2007-12-11
  • 打赏
  • 举报
回复
FileInputStream类的构造函数没有指定编码的,请问如何不指定编码,好不让调用本地的
yifuzhiming 2007-12-11
  • 打赏
  • 举报
回复
如何改?replaceGb2312是我的类名,楼上的是不是看错了?
数据兔 2007-12-11
  • 打赏
  • 举报
回复
FileInputStream read=new FileInputStream(path);
这句出问题了,你应该指定下编码,??出现了乱码,如果不做指定的话,直接调用本地的编码
liu_shuo 2007-12-11
  • 打赏
  • 举报
回复
把字符集GB2312改为UTF-8看看

62,615

社区成员

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

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