Java.io.File.length () returns 0

I am making an applet for transferring ftp files, and I need to know the size of the local file (to resume downloading). The problem is that File.length () returns 0.

A file exists (marked with File.exists ()) and has more than 0 bytes (at least on Windows).

I don't know where to look anymore, why length () returns 0.

Here is the piece of 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:

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

thanks

+10
source share
6 answers

The existence of the variable "outputStream" suggests that at the moment, you may already have opened the file for writing, and in this process you truncated it. Try to calculate the size before actually opening the file?

+9
source

The length in bytes of the file indicated by this abstract path name, or 0L if the file does not exist. Some operating systems may return 0L for path names denoting system objects, such as devices or channels.

+2
source

There is no reason in this code so I can understand why it should return 0 if it is not empty, are you doing anything else with this file?

If you opened the file somewhere else or recorded it, and the call length before you reset the recording (it may be in Java or in another place), then it may return 0. If you close and clear all writers to this file before checking its length, and you may have a different result.

+1
source

You probably do not have sufficient permissions to access the file ...

  • Try signing your applet. (Even a self-signed certificate will do.)
  • During development, you can modify the Java security policy to allow access to local files: see %JRE_HOME%/lib/security/java.policy , try adding

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

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

+1
source

You are using shoud:

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

instead

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

0
source

Sometimes there are problems with the OS when accurately returning the file size, even if the file is stable enough. I took a two-time approach to 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; } 
0
source

All Articles