请看下java代码

来源:百度知道 编辑:UC知道 时间:2024/06/30 07:24:06
import java.io.*;
public class FileUtil {
/**按照本地平台字符编码读取文件中的内容*/
public void readFile(String fileName)throws IOException{
readFile(fileName,null);
}
/**按照charSet指定的字符编码读取文件内容*/
public void readFile(String fileName,String charSet)throws IOException{
InputStream in=new FileInputStream(fileName);
InputStreamReader reader;
if(charSet==null)
reader=new InputStreamReader(in);
else
reader=new InputStreamReader(in,charSet);
BufferedReader br=new BufferedReader(reader);
String data;
while((data=br.readLine())!=null)
System.out.println(data);
br.close();
}
/**将一个文件中的内容拷贝到另一文件并进行相关的字符编码转换*/
public void copyFile(String from,String charsetFrom,String to,String charsetTo)
throws IOException{
InputStream in=new FileInputStream(from);

代码没问题的,如果D:\\test.txt确实是本地默认平台的字符编码的话
你检查一下那个文件是不是用了Unicode编码了,否则得话只好制定字符集了

Stream流读出来的都是二进制流,没有经过解码,具体的解决方案我也没碰到过.就不细细分析了.

用字节流本地默认编码集读中文是乱码的话,你用readFile("D:\\test.txt","gb2312")试试