日期:2014-05-20 浏览次数:21193 次
package com.walkman.exercise.one;
import java.io.*;
public class InputStreamTest {
public static void main(String[] args) {
// 先创建一个输入流;
FileInputStream fis = null;
// 记录字节长度;
int hasRead = 0;
// 声明一个字节数组来存字节。
byte buff[] = new byte[1024];// 换成3读就会出现乱码
try {
// 为流类赋值;
fis = new FileInputStream("InputStreamTest.java");
// 开始循环读取内容了;
while ((hasRead = fis.read(buff)) > 0) {
System.out.print(new String(buff));
}
} catch (IOException e) {
e.printStackTrace();
} finally {// (finally应该放哪呢,放这里老报错)
if (fis != null)
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
------解决方案--------------------
中文字符、英文字符都是“一个字符”,只不过占用空间不一样,你设置好编码格式,java能分辨出来,这样中英文字符就能“混合着”读出来了。。仅供参考哈