//读取二进制文件,字节输入流
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" );}
}
运行结果