Heim > Java > javaLernprogramm > Hauptteil

Java-Programm zum Ermitteln der Größe einer bestimmten Datei in Bytes, Kilobytes und Megabytes

WBOY
Freigeben: 2023-09-06 10:13:08
nach vorne
1367 Leute haben es durchsucht

Java-Programm zum Ermitteln der Größe einer bestimmten Datei in Bytes, Kilobytes und Megabytes

文件的大小是特定文件在特定存储设备(例如硬盘驱动器)上占用的存储空间量。文件的大小以字节为单位来衡量。在本节中,我们将讨论如何实现一个 java 程序来获取给定文件的大小(以字节、千字节和兆字节为单位)。

字节是数字信息的最小单位。一个字节等于八位。

  • 1 千字节 (KB) = 1,024 字节

  • 1 兆字节 (MB) = 1,024 KB

  • 千兆字节 (GB) = 1,024 MB 和

  • 1 太字节 (TB) = 1,024 GB。

文件的大小通常取决于文件的类型及其包含的数据量。以文本文档为例,文件的大小可能只有几千字节,而高分辨率图像或视频文件的大小可能有几千兆字节。

文件是信息的集合,可能包含文本信息、图像、音频、视频、程序代码等数据。任何软件应用程序都可以访问它们以执行读取、写入、更新、删除等操作。

语法

创建文件类对象

File file = new File("file_path");
Nach dem Login kopieren

创建BufferedStramInout类对象

BufferedInputStream input = new BufferedInputStream(new FileInputStream("filename"));
Nach dem Login kopieren

length() - 此方法返回文件的大小(以字节为单位)。

File file = new File("/example.txt");
long fileSize = file.length();
Nach dem Login kopieren

exists() - 此方法检查文件是否存在并返回布尔值。

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

read() - 此方法用于从输入流读取字节。

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

size() - 此方法返回文件的大小(以字节为单位)。

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

现在,我们将实现不同的 java 方法来查找给定文件的大小(以字节、千字节和兆字节为单位)。

方法 1:使用 java.io。套餐

在这种特殊方法中,我们将使用java.io包File类并使用不同的内置函数并获取文件的大小。

算法

  • 使用 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.");
      }
   }
}
Nach dem Login kopieren

输出

File size in bytes is equal to : 1048576 
File size in kilobytes is equal to : 1024
File size in megabytes is equal to : 1	
Nach dem Login kopieren

方法2:使用java.nio包

在这种特定方法中,我们将使用 java.nio 包的 Path 类,并使用不同的内置函数并获取文件的大小。

算法

  • 使用 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());
      }
   }
}
Nach dem Login kopieren

输出

File size in bytes is equal to : 1048576 
File size in kilobytes is equal to : 1024
File size in megabytes is equal to : 1
Nach dem Login kopieren

因此,在本文中,我们讨论了 Java 中获取给定文件大小(以字节、千字节和兆字节为单位)的不同方法。

Das obige ist der detaillierte Inhalt vonJava-Programm zum Ermitteln der Größe einer bestimmten Datei in Bytes, Kilobytes und Megabytes. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Verwandte Etiketten:
Quelle:tutorialspoint.com
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!