Java程式:獲取指定檔案的大小(位元組、千位元組和兆位元組)


檔案大小是指特定檔案在特定儲存裝置(如硬碟)上佔用的儲存空間量。檔案大小以位元組為單位衡量。

在本節中,我們將討論如何實現一個Java程式來獲取指定檔案的大小(位元組、千位元組和兆位元組)。

理解檔案大小和度量單位

位元組是數字資訊的最小單位。一個位元組等於八位。

  • 1 千位元組 (KB) = 1,024 位元組

  • 1 兆位元組 (MB) = 1,024 KB

  • 1 吉位元組 (GB) = 1,024 MB 以及

  • 1 太位元組 (TB) = 1,024 GB。

檔案的大小通常取決於檔案型別及其包含的資料量。例如,文字文件的大小可能只有幾千位元組,而高解析度影像或影片檔案的大小則可能為幾個吉位元組。

檔案是資訊的集合,可能包含文字資訊、影像、音訊、影片、程式程式碼等資料。任何軟體應用程式都可以訪問這些資料以執行讀取、寫入、更新、刪除等操作。

語法

建立檔案類物件

File file = new File("file_path");

建立BufferedInputStream類物件

BufferedInputStream input = new BufferedInputStream(new FileInputStream("filename"));

length() − 此方法返回檔案的大小(以位元組為單位)。

File file = new File("/example.txt");
long fileSize = file.length();

exists() − 此方法檢查檔案是否存在,並返回布林值。

File file = new File("/example.txt");
if (file.exists()) {
  System.out.println("File exists.");
}

read() − 此方法用於從輸入流中讀取位元組。

FileInputStream input = new FileInputStream(filePath)
byte[] buffer = new byte[10];
int bytesRead = input.read(buffer); 

size() − 此方法返回檔案的大小(以位元組為單位)。

Path path = Paths.get("/path/to/file");
long fileSize = Files.size(path);

現在,我們將實現不同的Java方法來查詢指定檔案的大小(位元組、千位元組和兆位元組)。

方法 1:使用 java.io 包

在這種方法中,我們將使用File 類(屬於java.io 包)以及不同的內建函式來獲取檔案的大小。

演算法

  • 使用 File 類建立一個檔案物件。

  • 使用 exists() 方法檢查檔案是否存在,如果存在,則使用 length() 方法查詢大小。

  • 列印以位元組、千位元組、兆位元組為單位的大小。

  • 如果檔案不存在,則列印“檔案未找到”。

示例

在這個例子中,我們使用File類建立了一個檔案物件,我們將使用exists()函式檢查檔案是否存在,如果存在,則使用length()函式計算檔案長度並將其儲存在‘sizebytes’變數中。我們將使用‘sizebytes’列印檔案大小(以位元組為單位)。千位元組大小為‘sizebytes’除以1024,兆位元組大小為‘sizebytes’除以1024*1024。

import java.io.File;
public class Main{
   public static void main(String[] args) {
      File f = new File("C:/example.txt");
      if (f.exists()) {
         long sizebytes = f.length();
         System.out.println("File size in bytes is equal to : " + sizebytes);
         System.out.println("File size in kilobytes is equal to : " + (sizebytes/ 1024));
         System.out.println("File size in megabytes is equal to : " + (sizebytes / (1024 * 1024)));
      } else {
         System.out.println("File not found.");
      }
   }
}

輸出

File size in bytes is equal to : 1048576 
File size in kilobytes is equal to : 1024
File size in megabytes is equal to : 1	

方法 2:使用 java.nio 包

在這種方法中,我們將使用Path類(屬於java.nio 包)以及不同的內建函式來獲取檔案的大小。

演算法

  • 使用 Path 類的 get() 方法建立一個路徑物件。

  • 使用 size() 方法查詢大小,並列印以位元組、千位元組、兆位元組為單位的大小。

  • 如果出現異常,則列印異常資訊。

示例

在這個例子中,我們使用Path類建立了一個檔案物件,我們將使用size()函式獲取檔案大小並將其儲存在‘sizebytes’變數中。我們將使用‘sizebytes’列印檔案大小(以位元組為單位)。千位元組大小為‘sizebytes’除以1024,兆位元組大小為‘sizebytes’除以1024*1024。

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Main {
   public static void main(String[] args) {
      Path p = Paths.get("C:/example.txt");
      try {
         long sizebytes = Files.size(p);
         System.out.println("File size in bytes is equal to : " + sizebytes);
         System.out.println("File size in kilobytes is equal to : " + (sizebytes/ 1024));
         System.out.println("File size in megabytes is equal to : " + (sizebytes / (1024 * 1024)));
      } catch (Exception e) {
         System.out.println("Error: " + e.getMessage());
      }
   }
}

輸出

File size in bytes is equal to : 1048576 
File size in kilobytes is equal to : 1024
File size in megabytes is equal to : 1

因此,在這篇文章中,我們討論了在Java中獲取指定檔案大小(位元組、千位元組和兆位元組)的不同方法。

更新於:2024年6月26日

3K+ 次瀏覽

開啟您的職業生涯

完成課程獲得認證

開始學習
廣告