How can I retrieve size of folder or file in Java?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This returns the length of the file in bytes or
0if the file does not exist. There is no built-in way to get the size of a folder, you are going to have to walk the directory tree recursively (using thelistFiles()method of a file object that represents a directory) and accumulate the directory size for yourself:WARNING: This method is not sufficiently robust for production use.
directory.listFiles()may returnnulland cause aNullPointerException. Also, it doesn’t consider symlinks and possibly has other failure modes. Use this method.