java 多种方式文件读取

 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。

posted @ 2012-03-20 20:45  谈笑风生膜法师  阅读(286)  评论(0编辑  收藏  举报