Get total size of file in bytes

JavaFileinputstream

Java Problem Overview


> 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?

Java Solutions


Solution 1 - Java

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

Solution 2 - Java

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

Solution 3 - Java

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

Solution 4 - Java

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionIllepView Question on Stackoverflow
Solution 1 - JavaSwapnilView Answer on Stackoverflow
Solution 2 - JavaHui ZhengView Answer on Stackoverflow
Solution 3 - JavaakaIDIOTView Answer on Stackoverflow
Solution 4 - JavaAvinash NairView Answer on Stackoverflow