//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" );}
}
运行结果