以下程序可以执行,读取一个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();
}
这句出问题了,你应该指定下编码,??出现了乱码,如果不做指定的话,直接调用本地的编码
str=str+new String(body);
}这里的改为new String("gb2312", i);