文件的大小是特定文件在特定存储设备(例如硬盘驱动器)上占用的存储空间量。文件的大小以字节为单位来衡量。在本节中,我们将讨论如何实现一个 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");
创建BufferedStramInout类对象
BufferedInputStream input = new BufferedInputStream(new FileInputStream("filename"));
length() - 此方法返回文件的大小(以字节为单位)。
File file = new File("/example.txt"); long fileSize = file.length();
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);
Path path = Paths.get("/path/to/file"); long fileSize = Files.size(path);
现在,我们将实现不同的 java 方法来查找给定文件的大小(以字节、千字节和兆字节为单位)。
方法 1:使用 java.io。套餐
在这种特殊方法中,我们将使用java.io包的File类并使用不同的内置函数并获取文件的大小。
算法
使用 File 类创建文件对象。
以字节、千字节、兆字节为单位打印大小。
示例
在此示例中,我们使用 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包
在这种特定方法中,我们将使用 java.nio 包的 Path 类,并使用不同的内置函数并获取文件的大小。
算法
示例
在此示例中,我们使用 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 中获取给定文件大小(以字节、千字节和兆字节为单位)的不同方法。
以上就是Java程序获取给定文件的大小(以字节、千字节和兆字节为单位)的详细内容,更多请关注编程之家其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。