[java] Get total size of file in bytes

Possible Duplicate:
java get file size efficiently

I have a File called filename which is located in E://file.txt.

FileInputStream fileinputstream = new FileInputStream(filename);

What I want to do is to calculate the size of this file in bytes. How can I have this done?

This question is related to java fileinputstream

The answer is


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().


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

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