forked from lokeshgupta1981/Core-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSize.java
More file actions
33 lines (24 loc) · 949 Bytes
/
FileSize.java
File metadata and controls
33 lines (24 loc) · 949 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.howtodoinjava.io;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import org.apache.commons.io.FileUtils;
public class FileSize {
public static void main(final String[] args) throws IOException {
File file = new File("c:/temp/demo.txt");
//1
long bytes = Files.size(file.toPath());
System.out.println(String.format("%,d Bytes", bytes));
System.out.println(String.format("%,d KB", bytes / 1024));
//2
bytes = file.length();
System.out.println(String.format("%,d Bytes", bytes));
//3
bytes = FileUtils.sizeOf(file);
System.out.println(String.format("%,d Bytes", bytes));
String displaySize = FileUtils.byteCountToDisplaySize(bytes);
System.out.println(displaySize);
System.out.println(FileUtils.byteCountToDisplaySize(2333444l)); //2 MB
System.out.println(FileUtils.byteCountToDisplaySize(2333444555l)); //2 GB
}
}