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。

转载于:https://www.cnblogs.com/dennisac/archive/2012/03/20/2408523.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值