Java.io.File.length()返回0 [英] Java.io.File.length() returns 0

查看:553
本文介绍了Java.io.File.length()返回0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

该文件存在(已通过File.exists()检查),并且具有超过0个字节(至少在Windows中).

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

我不知道还有什么地方可以找出为什么length()返回0.

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;
}

控制台中的结果是:

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

谢谢

推荐答案

变量"outputStream"的存在表明,此时,也许您已经打开了要写入的文件,并且在此过程中,您已经截断它.在实际打开文件之前尝试计算大小?

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?

这篇关于Java.io.File.length()返回0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆