使用Java操作二進位檔案 樣本

來源:互聯網
上載者:User

/*當我們對二進位的檔案處理的時候,我們應該使用FileInputStream和FileOutputStream。

當我們操作檔案的時候,可以首先使用File類得到對這個檔案的引用,例如
File file = new File("Idea.jpg");然後把file作為參數傳給FileInputStream或者FileOutputStream得到相應的輸入資料流或者輸出資料流。通常我們對檔案無非是進行讀寫,修改,刪除等操作。最重要的就是讀寫操作。當我們讀檔案的時候應該使用InputStream,寫檔案的時候使用OutputStream。read()方法是在InputStream中定義的,它是個抽象方法。InputStream當然也是個抽象類別,我們得到的執行個體都是它的子類,例如FileInputStream,子類如果不是抽象類別的話就要實現父類的抽象方法。在FileInputStream中不但實現了read()並且重載了這個方法提供了read(byte[] buffer)和read(byte[] buffer,int off,int length)兩個方法。下面詳細介紹一下:

    read()方法將讀取輸入資料流中的下一個位元組,並把它作為傳回值。傳回值在0-255之間,如果返回為-1那麼表示到了檔案結尾。用read()我們可以一個一個位元組的讀取並根據傳回值進行判斷處理。
while((ch = image.read())!=-1)
{
     System.out.print(ch);
     newFile.write(ch);
}

    read(byte[] buffer)會把流中一定長度的位元組讀入buffer中,傳回值為實際讀入buffer的位元組長度,如果返回-1表示已經到了流的末尾。
 while((ch = image.read(buffer))!=-1)
{
     System.out.println(ch);
     newFile.write(buffer);
}

    read(byte[] buffer,int off,int length)的意思是把流內length長度的位元組寫入以off為位移量的buffer內,例如off=7,length=100的情況下,這個方法會從流中讀100個位元組放到buffer[7]到buffer[106]內。傳回值為實際寫入buffer的位元組長度。
 while((ch = image.read(buffer,10,500))!=-1)
{
     System.out.println(ch);
     newFile.write(buffer,10,500);

}

    對上面的方法進行介紹的時候我們沒有考慮異常的情況,讀者應該參考API doc進行必要的瞭解。當我們對流操作的時候,有的時候我們可以對流進行標記和重設的操作,當然要流支援這樣的操作。參考一下mark(),reset()和markSupported()方法的說明。最後在使用結束後,確保關閉流,調用close()方法。由於FileOutputStream的write相關的方法和FileInptutStream的read()非常類似,因此不再多說。下面提供一個例子說明如何對二進位檔案進行操作,我們開啟一個JPEG格式的檔案,通過三種不同的方式讀取內容,並產生一個新的檔案。運行結束後你會發現這兩個檔案完全一樣!

*/

import java.io.*;
public class LinkFile{
 public static void main(String[] args) throws IOException{
  linkBinaryFile("chj.jpg");
 }
 private static void linkBinaryFile(String fileName) throws IOException{
  File imageFile = new File(fileName);
  if(!imageFile.exists()&&!imageFile.canRead()){
   System.out.println("can not read the image or the image file doesn't exists");
   System.exit(1);
  }
   long length = imageFile.length();
   int ch = 0;
   System.out.println(length);
   byte[] buffer = new byte[(int)length/7];
   InputStream image = new FileInputStream(imageFile);
 
   File file = new File("hello.jpg");
   if(!file.exists()){
  file.createNewFile();
   }
   FileOutputStream newFile = new FileOutputStream(file,true);
 
   boolean go = true;
   while(go){
     System.out.println("please select how to read the file:/n"+
     "1: read()/n2:read(byte[] buffer)/n3:read(byte[] buffer,int off,int len)/n");
     BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
     String line = br.readLine();
     if(line.equals("1")){
       while((ch = image.read())!=-1){
     System.out.print(ch);
     newFile.write(ch);
    }
    }else if(line.equals("2")){
   while((ch = image.read(buffer))!=-1){
        System.out.println(ch);
    newFile.write(buffer);
   }
  }else if(line.equals("3")){
   while((ch = image.read(buffer,10,500))!=-1){
    System.out.println(ch);
    newFile.write(buffer,10,500);
   }
   for(int i = 0;i<10;i++){
   System.out.print(buffer[i]);
   }
  }
  go = false;
 }
 image.close();
 newFile.close();
 }
}
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.