Get readable file size
import java.io.File;
public final class Main {
public static void main(String[] argv){
System.out.println(getReadableFileSize(10000));
System.out.println(getReadableFileSize(new File("yourFileName").length()));
}
public static String getReadableFileSize(final long fileSize) {
double l = fileSize * 1D;
String s = "";
if (l < 1000) {
s = fileSize + " B";
} else {
l = l / 1024D;
if (l < 1000) {
s = l + " KB";
} else {
l = l / 1024D;
if (l < 1000) {
s = l + " MB";
} else {
l = l / 1024D;
s = l + " GB";
}
}
}
return (s.length() - s.indexOf('.') <= 3 ? s : s.substring(0, s.indexOf('.') + 3))
+ s.substring(s.indexOf(" "), s.length());
}
}
Home
Java Book
Runnable examples
Java Book
Runnable examples
IO File:
- Compare File Dates
- Compress files using with ZIP
- Concatenate files
- Copy a File with NIO FileChannel and ByteBuffer
- Copy a file with FileReader and FileWriter
- Copy a file with InputStream and OutputStream
- Copy a file and overwrite
- Delete a file
- Delete File Recursively
- Get readable file size
- Move a file
- Rename a file
- Report a file's status
- Search a file by regular expressions
- Touch a file: set File Last Modified Time