以字节流读入文件(内容包括中文)时显示乱码解决思路
以字节流读入文件(内容包括中文)时显示乱码
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. 有没有更好的读取方法
------解决方案--------------------
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());