天天看点

java148-三个参数的输入流

//3个参数读取二进制文件
     import java.io.*;
      
     public class FileManage {
         public static void readBitFile(String filename){
             //1建立目标要读取的文件对象
             File file=new File( filename );
             //2基于目标对象建立输入流
             InputStream in=null;
             if(file.exists()){//如果文件存在,创建文件输入流
                 System.out.println( "文件的长度"+file.length() );
                 try {
                     in = new FileInputStream( file );//使用子类inputstream输入流
                     int count = 0;//读取的字节数
                     byte []bys=new byte[124];//临时存储读取的二进制数据
                     while((count=in.read(bys,0,bys.length))!=-1) {
                         String s=new String(bys,0,count);
                         System.out.print( s);
                     }
                 }catch (FileNotFoundException e){
                     e.printStackTrace();
                 }catch (IOException e){
                     e.printStackTrace();
                 }finally {
                     try {
                         in.close();
                         System.out.println( "关闭成功" );
                     }catch (IOException e){
                         e.printStackTrace();
                     }
                 }
             }
             //读取文件内容
      
             //关闭输入流
         }
     }测试类
    public class test93 {
         public static void main(String[] args){
             FileManage.readBitFile( "e:/1.txt" );
         }
     }      

运行结果

java148-三个参数的输入流

继续阅读