java147-字节输入流

    import java.io.*;
     
    public class FileMana {
        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输入流
                    long  count = 0;//读取的字节数
                    byte []bys=new byte[124];//临时存储读取的二进制数据
                    while((count=in.read(bys))!=-1) {
                        String s=new String(bys);
                        System.out.print( s);
                    }
                }catch (FileNotFoundException e){
                    e.printStackTrace();
                }catch (IOException e){
                    e.printStackTrace();
                }finally {
                    try {
                        in.close();
                    }catch (IOException e){
                        e.printStackTrace();
                    }
                }
            }
            //读取文件内容
     
            //关闭输入流
        }
    }

测试类

    public class test92 {
        public static void main(String[] args){
            FileMana.readBitFile( "e:/1.txt" );
        }
    }

运行结果

 

posted @ 2022-07-05 18:43  前端导师歌谣  阅读(30)  评论(0)    收藏  举报