Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get total size of file in bytes [duplicate]

People also ask

How can I get file size in bytes?

Using File#length() method A simple solution is to call the File#length() method that returns the size of the file in bytes. To get the file size in MB, you can divide the length (in bytes) by 1024 * 1024 .

Can you find the file size the number of bytes using the file class?

Java get file size using File classJava File length() method returns the file size in bytes.

How do I see multiple file sizes in Linux?

To get the total size of a directory in Linux, you can use the du (disk-usage) command.


You can use the length() method on File which returns the size in bytes.


You don't need FileInputStream to calculate file size, new File(path_to_file).length() is enough. Or, if you insist, use fileinputstream.getChannel().size().


You can do that simple with Files.size(new File(filename).toPath()).


public static void main(String[] args) {
        try {
            File file = new File("test.txt");
            System.out.println(file.length());
        } catch (Exception e) {
        }
    }