3

I'm trying to get the length (in bytes) of some files. The problem is that I get a zero for their length while they are not of length zero (I checked!)

Moreover, every other file's method I'm using on these files works just fine. so it's just the issue with the length.

why this happening?

thank you.

    //add all files names in the directory into array
    String[] files = new File(sourcedir).list();
    filesNamesList.addAll(Arrays.asList(files));
    filesNamesList.removeIf(name -> ((new File(sourcedir + PATH_BACK_SLASH + name))
            .isDirectory()));

    for (String f:files){
        File e =  new File(f);
        System.out.println((e).length());

    }
}
Austin Schaefer
  • 695
  • 5
  • 19
Hadar
  • 39
  • 3
  • 1
    I don't think it has anything to do with your problem but when removing the directories from your list, try using the constructor `File(sourcedir, name)` – Robert Kock Jun 18 '19 at 09:49
  • the output was: 0 0 0 0 0 0 0 0 0 0 0 0 (each 0 in a different line, for 12 different file objects) – Hadar Jun 18 '19 at 09:53
  • Have a look at : https://stackoverflow.com/questions/2149785/get-size-of-folder-or-file – Alin Jun 18 '19 at 10:03
  • Looks like duplication of https://stackoverflow.com/questions/6962575/java-io-file-length-returns-0 – Oleks Jun 18 '19 at 10:15
  • Does this answer your question? [Get size of folder or file](https://stackoverflow.com/questions/2149785/get-size-of-folder-or-file) – Soorya J Jun 22 '20 at 06:17

4 Answers4

1

It is worth checking the Absolute path of a file using File.getAbsolutePath() to make sure whether the function is given with the right path to the file or not.

SIVA KUMAR
  • 105
  • 1
  • 2
1

The list() method that you're using lists the file names, not their absolute paths.

So your code lists all file names in sourcedir (not their full paths), then looks for those same file names in the current directory you're running your program from. That's why length() returns 0 for all those files (as per the docs, it'll return 0 if the file doesn't exist.)

If you instead want a list of all the absolute paths, then you can do that concisely using streams like so:

List<String> files = Arrays.stream(new File("C:\\users\\michael\\desktop").listFiles())
        .map(f -> f.getAbsolutePath())
        .collect(Collectors.toList());

However, if all these files are only ever going to be from the same directory, then use new File(sourcedir, name) in your for loop (this is a better alternative than the new File(sourcedir + PATH_BACK_SLASH + name) you use elsewhere.)

Michael Berry
  • 70,193
  • 21
  • 157
  • 216
0

Your file paths are possibly incorrect.

Java's File.length() Javadoc states that length() may return 0L if the file is not found.

The length, in bytes, of the file denoted by this abstract pathname, or 0L if the file does not exist. Some operating systems may return 0L for pathnames denoting system-dependent entities such as devices or pipes."

Therefore, if you're certain your files have content in them, then you are likely not finding them correctly. Double check your file paths and try again.

Also, the javadoc recommends using Files.readAttributes() if you need more detailed error information; I echo their sentiment here.

Austin Schaefer
  • 695
  • 5
  • 19
  • 1
    thanks! I should have used the absolute path of the files and not just their names. thank you!! – Hadar Jun 18 '19 at 10:14
  • You're welcome. If the answer worked for you, please consider upvoting. And definitely click the check mark to accept it as the correct answer. – Austin Schaefer Jun 18 '19 at 10:15
-1

The problem is that those files does not contain any contents in them. So you are getting 0 as the output