1 import java.io.File;
2 import java.io.InputStream;
3 import java.io.FileInputStream;
4 //import org.omg.CORBA_2_3.portable.OutputStream;
5 public class dm {
6 public static void main(String[] args) throws Exception {
7 File f=new File("d:"+File.separator+"te.txt");
8 InputStream out=new FileInputStream(f);
9 byte b[]=new byte[(int)f.length()];
10 out.read(b);
11 out.close();
12 System.out.println("内容为:"+new String(b));
13 }
14 }
关键是第九行:
1.for(int i=0;i<b.length;i++){
b[i]=(byte)out.read();
}
已经知道数组大小,开展输出。
2.while(temp=out.read())!=-1){
b[len]=(byte)temp;
len++;
}
文件读到末尾了,返回值为-1。