利用JAVA如何實現(xiàn)讀取二進(jìn)制文件的內(nèi)容

這篇文章運(yùn)用簡單易懂的例子給大家介紹利用JAVA如何實現(xiàn)讀取二進(jìn)制文件的內(nèi)容,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

創(chuàng)新互聯(lián)建站是一家集網(wǎng)站建設(shè),成都企業(yè)網(wǎng)站建設(shè),成都品牌網(wǎng)站建設(shè),網(wǎng)站定制,成都網(wǎng)站建設(shè)報價,網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,成都網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。

JAVA中讀取文件內(nèi)容的方法有很多,比如按字節(jié)讀取文件內(nèi)容,按字符讀取文件內(nèi)容,按行讀取文件內(nèi)容,隨機(jī)讀取文件內(nèi)容等方法,本文就以上方法的具體實現(xiàn)給出代碼,需要的可以直接復(fù)制使用

public class ReadFromFile {
 /**
  * 以字節(jié)為單位讀取文件,常用于讀二進(jìn)制文件,如圖片、聲音、影像等文件。
  */
 public static void readFileByBytes(String fileName) {
  File file = new File(fileName);
  InputStream in = null;
  try {
   System.out.println("以字節(jié)為單位讀取文件內(nèi)容,一次讀一個字節(jié):");
   // 一次讀一個字節(jié)
   in = new FileInputStream(file);
   int tempbyte;
   while ((tempbyte = in.read()) != -1) {
    System.out.write(tempbyte);
   }
   in.close();
  } catch (IOException e) {
   e.printStackTrace();
   return;
  }
  try {
   System.out.println("以字節(jié)為單位讀取文件內(nèi)容,一次讀多個字節(jié):");
   // 一次讀多個字節(jié)
   byte[] tempbytes = new byte[100];
   int byteread = 0;
   in = new FileInputStream(fileName);
   ReadFromFile.showAvailableBytes(in);
   // 讀入多個字節(jié)到字節(jié)數(shù)組中,byteread為一次讀入的字節(jié)數(shù)
   while ((byteread = in.read(tempbytes)) != -1) {
    System.out.write(tempbytes, 0, byteread);
   }
  } catch (Exception e1) {
   e1.printStackTrace();
  } finally {
   if (in != null) {
    try {
     in.close();
    } catch (IOException e1) {
    }
   }
  }
 }

 /**
  * 以字符為單位讀取文件,常用于讀文本,數(shù)字等類型的文件
  */
 public static void readFileByChars(String fileName) {
  File file = new File(fileName);
  Reader reader = null;
  try {
   System.out.println("以字符為單位讀取文件內(nèi)容,一次讀一個字節(jié):");
   // 一次讀一個字符
   reader = new InputStreamReader(new FileInputStream(file));
   int tempchar;
   while ((tempchar = reader.read()) != -1) {
    // 對于windows下,\r\n這兩個字符在一起時,表示一個換行。
    // 但如果這兩個字符分開顯示時,會換兩次行。
    // 因此,屏蔽掉\r,或者屏蔽\n。否則,將會多出很多空行。
    if (((char) tempchar) != '\r') {
     System.out.print((char) tempchar);
    }
   }
   reader.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
  try {
   System.out.println("以字符為單位讀取文件內(nèi)容,一次讀多個字節(jié):");
   // 一次讀多個字符
   char[] tempchars = new char[30];
   int charread = 0;
   reader = new InputStreamReader(new FileInputStream(fileName));
   // 讀入多個字符到字符數(shù)組中,charread為一次讀取字符數(shù)
   while ((charread = reader.read(tempchars)) != -1) {
    // 同樣屏蔽掉\r不顯示
    if ((charread == tempchars.length)
      && (tempchars[tempchars.length - 1] != '\r')) {
     System.out.print(tempchars);
    } else {
     for (int i = 0; i < charread; i++) {
      if (tempchars[i] == '\r') {
       continue;
      } else {
       System.out.print(tempchars[i]);
      }
     }
    }
   }

  } catch (Exception e1) {
   e1.printStackTrace();
  } finally {
   if (reader != null) {
    try {
     reader.close();
    } catch (IOException e1) {
    }
   }
  }
 }

 /**
  * 以行為單位讀取文件,常用于讀面向行的格式化文件
  */
 public static void readFileByLines(String fileName) {
  File file = new File(fileName);
  BufferedReader reader = null;
  try {
   System.out.println("以行為單位讀取文件內(nèi)容,一次讀一整行:");
   reader = new BufferedReader(new FileReader(file));
   String tempString = null;
   int line = 1;
   // 一次讀入一行,直到讀入null為文件結(jié)束
   while ((tempString = reader.readLine()) != null) {
    // 顯示行號
    System.out.println("line " + line + ": " + tempString);
    line++;
   }
   reader.close();
  } catch (IOException e) {
   e.printStackTrace();
  } finally {
   if (reader != null) {
    try {
     reader.close();
    } catch (IOException e1) {
    }
   }
  }
 }

 /**
  * 隨機(jī)讀取文件內(nèi)容
  */
 public static void readFileByRandomAccess(String fileName) {
  RandomAccessFile randomFile = null;
  try {
   System.out.println("隨機(jī)讀取一段文件內(nèi)容:");
   // 打開一個隨機(jī)訪問文件流,按只讀方式
   randomFile = new RandomAccessFile(fileName, "r");
   // 文件長度,字節(jié)數(shù)
   long fileLength = randomFile.length();
   // 讀文件的起始位置
   int beginIndex = (fileLength > 4) &#63; 4 : 0;
   // 將讀文件的開始位置移到beginIndex位置。
   randomFile.seek(beginIndex);
   byte[] bytes = new byte[10];
   int byteread = 0;
   // 一次讀10個字節(jié),如果文件內(nèi)容不足10個字節(jié),則讀剩下的字節(jié)。
   // 將一次讀取的字節(jié)數(shù)賦給byteread
   while ((byteread = randomFile.read(bytes)) != -1) {
    System.out.write(bytes, 0, byteread);
   }
  } catch (IOException e) {
   e.printStackTrace();
  } finally {
   if (randomFile != null) {
    try {
     randomFile.close();
    } catch (IOException e1) {
    }
   }
  }
 }

 /**
  * 顯示輸入流中還剩的字節(jié)數(shù)
  */
 private static void showAvailableBytes(InputStream in) {
  try {
   System.out.println("當(dāng)前字節(jié)輸入流中的字節(jié)數(shù)為:" + in.available());
  } catch (IOException e) {
   e.printStackTrace();
  }
 }

 public static void main(String[] args) {
  String fileName = "C:/temp/newTemp.txt";
  ReadFromFile.readFileByBytes(fileName);
  ReadFromFile.readFileByChars(fileName);
  ReadFromFile.readFileByLines(fileName);
  ReadFromFile.readFileByRandomAccess(fileName);
 }
}

關(guān)于利用JAVA如何實現(xiàn)讀取二進(jìn)制文件的內(nèi)容就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

新聞名稱:利用JAVA如何實現(xiàn)讀取二進(jìn)制文件的內(nèi)容
網(wǎng)站鏈接:http://bm7419.com/article30/jjsoso.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開發(fā)、標(biāo)簽優(yōu)化、營銷型網(wǎng)站建設(shè)、面包屑導(dǎo)航、Google、網(wǎng)頁設(shè)計公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名