Java基础知识强化之IO流笔记27:FileInputStream读取数据一次一个字节数组byte[ ]

1. FileInputStream读取数据一次一个字节数组byte[ ]

 使用FileInputStream一次读取一个字节数组:

  int read(byte[]  b)

  返回值:返回值其实是实际读取的字节个数 .

 

2. 代码示例:

 1 package com.himi.fileinputstream;
 2 
 3 import java.io.FileInputStream;
 4 import java.io.IOException;
 5 
 6 
 7 
 8 /**
 9  * 
10  * 使用FileInputStream一次读取一个字节数组:int read(byte[] b)
11  * 返回值:返回值其实是实际读取的字节个数
12  * 
13  */
14 
15 
16 public class FileInputStreamDemo1 {
17 
18     public static void main(String[] args) throws IOException {
19         FileInputStream fis = new FileInputStream("a.txt");
20     //开发时候最终版本代码
21         byte[] bys = new byte[1024];
22         int len = 0;
23         while((len = fis.read(bys)) != -1) {
24             System.out.print(new String(bys,0,len));
25         }
26         
27     }
28 
29 }

这里可以查看FileInputStream的API中的read(byte[]  b),如下:

运行效果,如下:

posted on 2015-10-08 18:46  鸿钧老祖  阅读(416)  评论(0编辑  收藏  举报

导航