Java.io.File.length() 返回 0

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6962575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 18:02:19  来源:igfitidea点击:

Java.io.File.length() returns 0

javafile

提问by Proen?a

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.

我正在做一个用于 ftp 文件传输的小程序,我需要知道本地文件的大小(用于下载简历)。问题是 File.length() 返回 0。

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

该文件存在(使用 File.exists() 检查),并且有 0 个以上的字节(至少在 Windows 中)。

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

我不知道去哪里寻找 length() 返回 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

谢谢

采纳答案by Ernest Friedman-Hill

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?

变量“outputStream”的存在表明此时,您可能已经打开文件进行写入,并且在此过程中,您已经截断了它。在实际打开文件之前尝试计算大小?

回答by Michael Berry

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?

该代码中没有理由我可以理解为什么如果它不为空它应该返回 0,您是否在其他地方对该文件进行了任何操作?

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.

如果您在其他地方打开了文件,或者正在写入文件并在刷新写入器之前调用 length(这可能在 Java 或其他地方),则它可能返回 0。如果您关闭并将所有写入器刷新到该写入器文件之前检查其长度,您可能会得到不同的结果。

回答by MateusR

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

此抽象路径名表示的文件的长度(以字节为单位),或者0L如果文件不存在。 某些操作系统可能会返回0L表示系统相关实体(例如设备或管道)的路径名。

回答by Valters Vingolds

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

  1. 尝试签署您的小程序。(即使是自签名证书也可以。)
  2. 开发时可以修改Java安全策略允许访问本地文件:查看%JRE_HOME%/lib/security/java.policy,尝试添加

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

  3. 尝试使用新的 JNLP API 来访问本地文件(虽然只有 Java5...)

回答by KAMAL VERMA

You shoud use :

你应该使用:

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

文件文件=新文件(uri.getPath());

instead of

代替

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

文件文件=新文件(uri.toString());

回答by bigbadmouse

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

有时在准确返回文件大小时会出现操作系统问题,即使文件非常稳定。如果 File 失败,我采用了使用 File 和 NIO 的双重方法

     long discoveredFileLength = discoveredFile.length();
     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;
    }