Java.io.File.length() returns 0

2020-06-12 03:06发布

问题:

I'm doing a applet for ftp file transfers and I need to know the size of a local file (for download resumes). The problem is the File.length() returns 0.

The file exists (checked with File.exists()), and has more than 0 bytes (in Windows at least).

I don't know where more to look to find out why length() is returning 0.

Here is part of the code and the result.

long fileOffset = 0;

if(localfile.exists()){
    fileOffset = localfile.length();
    System.out.println("The file " + localfile.getAbsolutePath() + " has " + localfile.length() +" in size");
    System.out.println("Resume at: " + fileOffset);
    outputStream.skip(fileOffset);
    ftp.setRestartOffset(fileOffset);
    count = fileOffset;
}

And the result in the console is:

The file D:\test\About Downloads.pdf has 0 in size
Resume at: 0

Thanks

回答1:

The existence of the variable "outputStream" suggests that at this point, perhaps you've already opened the file for writing, and in the process, you've truncated it. Try computing the size before actually opening the file?



回答2:

There's no reason in that code that I can see why it should return 0 if it's not empty, are you doing anything elsewhere with that file?

If you've got the file open somewhere else, or are writing to it and call length before you've flushed the writer (this could be in Java or elsewhere) then it may return 0. If you close and flush all writers to that file before checking its length and you may have a different result.



回答3:

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.



回答4:

You probably don't have enough permissions to access the file at all...

  1. Try signing your applet. (Even self-signed certificate will do.)
  2. While developing, you can modify the Java security policy to allow accessing local files: see %JRE_HOME%/lib/security/java.policy, try adding

    permission java.io.FilePermission "<<ALL FILES>>", "read,write";

  3. Try using the new JNLP API to access local files (Java5 only though...)



回答5:

You shoud use :

File file=new File(uri.getPath());

instead of

File file=new File(uri.toString());



回答6:

There are sometimes OS issues in returning file size accurately, even when the file is quite stable. I adopted a two fold approach of using File and NIO if File failed

     long discoveredFileLength = discoveredFile.length();
    if (discoveredFileLength == 0) {
        final Path discoveredFilePath = Paths.get(discoveredFile.getAbsolutePath());
        FileChannel discoveredFileChannel = null;
        try {
            discoveredFileChannel = FileChannel.open(discoveredFilePath);
            discoveredFileLength = discoveredFileChannel.size();
        }
        catch (IOException e2) {
        }
    }

    if (discoveredFileLength <= 0) {
        logErrors(discoveredFile.getName() + " " + discoveredFileLength  + " COULD NOT BE PROCESSED (length <= 0)");
        _reporter.increaseFilesFailed();
        return;
    }


标签: java file