//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" );
}
}
運作結果