读书人

以字节流读入文件(内容包括中文)时显示

发布时间: 2012-03-22 17:43:57 作者: rapoo

以字节流读入文件(内容包括中文)时显示乱码
public static void main(String[] args) throws IOException
{

byte[] bytes = new byte[3]; //

File file = new File("c:/a.txt");

FileInputStream fis = new FileInputStream(file);

StringBuffer request1 = new StringBuffer();

while(fis.read(bytes)!= -1)
{
request1.append(new String(bytes));

}

System.out.println(request1.toString());

}
问题:1. java是如何处理中文字的(编码方式是什么)
2. 若数组长度是2的倍数则能正常显示,是不是中文字是占两个字节的
3. 有没有更好的读取方法

[解决办法]

Java code
FileReader fr = new FileReader("c:/a.txt");BufferedReader br = new BufferedReader (fr)String line = "";StringBuffer request1 = new StringBuffer();while((line = br.readline()) != null){    request1.append(line);  }System.out.println(request1.toString()); 

读书人网 >J2SE开发

热点推荐