javaIO流(2):文件字节输入流

一:输入流步骤


1.设定输入流的源
2.创建指向源的输入流
3.让输入流读取源中的数据
4.关闭输入流


二:具体用法


1.构造方法
FileInputStream(File  file);通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的 File 对象 file 指定。


FileInputStream(String name);通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的路径名 name 指定。


参数name和file指定的文件称为输入流的源
FileInputStream输入流打开一个到达文件的通道(源是指该文件,输入流指向这个文件),输入流出现的异常是IOException异常


2.案例:读取名为hello.txt的文件


方法一:
try{
FileInputStream in=new  FileInputStream(“hello.txt”);//创建指向文件的流
}
catch(IOException  e){
System.out.println(“File  read  error:”+e);
}


方法二:
File  f=new  File(“hello.txt”);//创建输入流的源
try{
FileInputStream  in=new  FileInputStream(f);//创建指向源的输入流
}
catch(IOException  e){
System.out.println(“File  read  error:”+e);
}


3.使用输入流读取字节


文件字节流可以调用从父类继承的read方法顺序的读取文件,只要不关闭流,每次调用read方法就顺序的读取文件中的其余内容,直到文件的末尾或者文件的字节流被关闭。


int  read()输入流调用该方法从源中读取单个字节的数据,该方法返回字节值(0~225之间的一个整数),如果未读出字节就返回—1
int  read(byte  b[]) 输入流调用该方法从源中试图读取b.length个字节到数组b中,返回实际读取的字节数,如果达到文件的末尾返回-1.
int  read(byte  b[],int  off,int  len)输入流调用该方法,从源中试图读取len个字节到数组b中,并返回实际读取的字节数目,如果达到文件末尾,则返回-1,参数off指定从字节数组的某个位置开始存放读取的数据。
FileInputStream流顺序的读取文件,只要不关闭流,每次调用read方法就顺序取源中的其余内容。


4.关闭流
输入流提供关闭方法close();




三:案例

[java]  view plain copy print ?
  1. <span style="font-size:14px;">public static void main(String[] args) {  
  2.         // TODO Auto-generated method stub  
  3.         int n=-1;  
  4.         byte a[]=new byte[100];  
  5.         try {  
  6.             File f=new File("Demo2.txt");  
  7.             InputStream in=new FileInputStream(f);  
  8.             while((n=in.read(a, 0100))!=-1){  
  9.                 String s=new String(a,0,n);  
  10.                 System.out.println(s);  
  11.             }  
  12.             in.close();  
  13.         } catch (IOException e) {  
  14.             // TODO Auto-generated catch block  
  15.             e.printStackTrace();  
  16.         }  
  17.     }</span>  


四:案例二

[java]  view plain copy print ?
  1. <span style="font-size:14px;">public class Demo3 {  
  2.     public static void main(String[] args) {  
  3.         // 1.获取文件对象  
  4.         File file=new File("D:\\IO.txt");  
  5.         InputStream is=null;  
  6.         try {  
  7.             //2.建立输入流  
  8.             is=new FileInputStream(file);  
  9.             byte [] buf=new byte[1024];  
  10.             //3.读取文件内容,放到read(byte  b[])放到数组中  
  11.             is.read(buf);  
  12.             System.out.println(new String(buf));  
  13.         } catch (Exception e) {  
  14.             e.printStackTrace();  
  15.         }finally{  
  16.             //4.关闭流对象  
  17.             if(is!=null){  
  18.                 try {  
  19.                     is.close();  
  20.                 } catch (IOException e) {  
  21.                     // TODO Auto-generated catch block  
  22.                     e.printStackTrace();  
  23.                 }  
  24.             }  
  25.         }  
  26.     }  
  27. }</span>  

上面代码存在缺点,受到byte数组开辟空间大小的限制,开辟动态数组空间,根据文件大小决定,使用read(),一个字节一个字节读取。

[java]  view plain copy print ?
  1. <span style="font-size:14px;"><span style="white-space:pre">                </span>//1.获取文件对象  
  2.                 File file=new File("D:\\IO.txt");  
  3.                 InputStream is=null;  
  4.                 try {  
  5.                     //2.建立输入流  
  6.                     is=new FileInputStream(file);  
  7.                     //固定字节数组长度  
  8.                     //byte [] buf=new byte[1024];  
  9.                     //3.读取文件内容  
  10.                     //is.read(buf);  
  11.                       
  12.                     //动态数组  
  13.                     //byte [] buf=new byte[(int)file.length()];  
  14.                     //is.read(buf);  
  15.                     byte [] buf=new byte[1024];  
  16.                     int len=0;  
  17.                     StringBuffer buffer=new StringBuffer();  
  18.                     while((len=is.read(buf))!=-1){  
  19.                         String str=new String(buf);  
  20.                         buffer.append(str);  
  21.                     }  
  22.                     System.out.println(new String(buf));  
  23.                 } catch (Exception e) {  
  24.                     // TODO Auto-generated catch block  
  25.                     e.printStackTrace();  
  26.                 }  
  27.                 finally  
  28.                 {  
  29.                     //4.关闭流对象  
  30.                     if(is!=null)  
  31.                     {  
  32.                         try  
  33.                         {  
  34.                             is.close();  
  35.                         }  
  36.                         catch(IOException e)  
  37.                         {  
  38.                             e.printStackTrace();  
  39.                         }  
  40.                     }  
  41.                 }  
  42.             }</span>  
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值