Java程序输出txt文件内容时中文乱码怎么解决?

程序如下:
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class TestFileInput {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
File f=new File("E:/student.txt");//创建文件路径
FileInputStream fis = null;//创建FileInputStream类的对象
try
{
fis = new FileInputStream(f);//由于File类不支持文件的读写,只表示文件的路径,所以需要通过FileInputStream类
byte[] bytes=new byte[1024];//定义字节为1024大小的数组
int n=0;
System.out.println("E盘Student文件内容如下:");
while((n=fis.read(bytes))!=-1)//通过read方法每次读取bytes大小的数据,当读取到文件末尾时返回-1
{

String s=new String(bytes,0,n);//将字节数组中的内容读取到字符串中

System.out.println(s);
}

}catch (Exception e){
e.printStackTrace();
}finally{
try{
fis.close();
}catch (IOException e){
e.printStackTrace();
}
}
}

}

运行结果是:
E盘Student文件内容如下:
My name is ������
怎么解决?

第1个回答  2015-11-28
既然是输出的不是二进制文件,为什么不用FileWriter+BufferedWriter呢,这样就不会有乱码了
第2个回答  2015-11-28
String s = new String(bytes, 0, n,"gbk");本回答被提问者采纳