- byte[] flush =new byte[n] // 字节数组充当缓冲容器
- .read(flush) // 每次返回读取的 n 个字节, 当数据字节数不够时, 返回实际字节数
- int len=-1; // 接受 read(flush) 返回的实际长度
- String s= new String(flush,0,len) // 解码操作, len 必须为实际大小, 否则多余长度会返回垃圾字符
- public static void main(String[]args)
- {
- // 创建源
- File f=new File("C:/Users/10853/eclipse-workspace/hell/src/hell/abc");
- InputStream is =null;
- // 选择流
- try {
- is =new FileInputStream(f);
- // 操作 (读取), 分段读取
- byte[] flush =new byte[3]; // 缓冲容器
- int len=-1; // 接收长度
- while((len=is.read(flush))!=-1) //is.read(car) 每次读取三个字节, 读取完毕会返回 - 1,
- // 当数据不够 3 时, 将返回实际数据个数
- {
- String s =new String(flush,0,len); // 必须返回实际的大小, 否则多余的长度会返回垃圾字符
- System.out.println(s);
- }
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }finally
- {
- try {
- if(null!=is)
- {
- is.close();
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
来源: http://www.bubuko.com/infodetail-3133188.html