Java FileSize-class And Method Code Example


Here is an example of how the FileSize class from the logback library can be used:

import ch.qos.logback.core.util.FileSize;

public class MyClass {
    public static void main(String[] args) {
        FileSize fileSize = new FileSize(1024);
        System.out.println("File size: " + fileSize.getSize() + " bytes");
        System.out.println("File size: " + fileSize.getHumanReadableUnits() + " KB");
    }
}

In this example, MyClass uses an instance of FileSize to convert a file size in bytes to a human-readable format. The constructor of the FileSize class takes a single argument, which is the file size in bytes. The getSize method returns the file size in bytes, and the getHumanReadableUnits method returns the file size in a human-readable format (e.g. "1 KB", "10 MB", etc.).

You can also use getHumanReadable to get the file size in human-readable format.

FileSize fileSize = new FileSize(1024);
System.out.println("File size: " + fileSize.getHumanReadable()); 

This will return the file size in format like 1.00KB.

You can also use parse method to parse string representation of filesize.

FileSize fileSize = FileSize.parse("1.5MB");

This will return FileSize object with filesize equivalent to 1.5 MB.