用Java获取文件的实际大小?

时间:2023-02-09 15:19:49

I stuck into a problem.

我遇到了一个问题。

  1. I started downloading a file from torrent. the Total Size of the File is 699 MB.
  2. 我开始从torrent下载文件。文件总大小为699 MB。

  3. But after a minute i stop my torrent client, torrent client shows 7 MB of data has been downloaded, I have only 1 File in Queue.
  4. 但是一分钟后我停止了我的torrent客户端,torrent客户端显示已经下载了7 MB的数据,我在队列中只有1个文件。

  5. Using Java IO function i calculated length of file and it show 699 MB of Size
  6. 使用Java IO函数我计算文件的长度,它显示699 MB的大小

File file= new File("D:/Easy.A.2010.BDRip.XviD-iMBT/easy.a.2010.bdrip.xvid-imbt.avi");

文件文件=新文件(“D:/Easy.A.2010.BDRip.XviD-iMBT/easy.a.2010.bdrip.xvid-imbt.avi”);

System.out.println("File Size="+file.length());

// After converting to MBs i got it is 699 MB

//转换为MB后,我得到它是699 MB

at later phase i am splitting file into different equal size parts and uploading to server. as the file size is not actual size of the file so i got Exception

在后期我将文件分成不同大小的部分并上传到服务器。因为文件大小不是文件的实际大小所以我得到了Exception

java.lang.NegativeArraySizeException

Please tell me how can i get actual size of the file, or get information about the file that it is complete file or not.

请告诉我如何获得文件的实际大小,或获取有关文件是否完整的文件的信息。

Thanks

3 个解决方案

#1


2  

I am not sure if this is relevant, but all the torrent clients I have used create files of the final size at the beginning of the download.

我不确定这是否相关,但我使用过的所有torrent客户端都会在下载开始时创建最终大小的文件。

i.e for the 699 MB movie, a blank 699 MB file is created right at the start, and the 7 MB downloaded only means that 7 MB from those blanks has been filled in, but the file size on disk remains 699 MB all throughout the download.

即对于699 MB的电影,在开始时创建一个空白的699 MB文件,而下载的7 MB仅表示已填写这些空白的7 MB,但在整个下载过程中磁盘上的文件大小仍为699 MB 。

#2


1  

Angit is right, torrent client crates blank file with size = torrent_file and if you download 7MB then it fills the blank file with that and if you want to calculate first check to see if file that torrent client creates is filled with blank then you can calculate how much blanks or non blanks you have and after that is easy :).

Angit是对的,torrent客户端包装空白文件,大小= torrent_file,如果你下载7MB,那么它用空白文件填充,如果你想先计算一下,看看你的torrent客户创建的文件是否填充空白,那么你可以计算你有多少空白或非空白,之后很容易:)。

#3


1  

Make up your mind. Is your question about file sizes or the NegativeArraySizeException? File.length() gives you the length of the file as per the last time the directory was updated. If the file is still open and still being extended this information may not be current.

下定决心。您对文件大小或NegativeArraySizeException有疑问吗? File.length()为您提供上次更新目录时文件的长度。如果文件仍处于打开状态且仍在扩展,则此信息可能不是最新的。

#1


2  

I am not sure if this is relevant, but all the torrent clients I have used create files of the final size at the beginning of the download.

我不确定这是否相关,但我使用过的所有torrent客户端都会在下载开始时创建最终大小的文件。

i.e for the 699 MB movie, a blank 699 MB file is created right at the start, and the 7 MB downloaded only means that 7 MB from those blanks has been filled in, but the file size on disk remains 699 MB all throughout the download.

即对于699 MB的电影,在开始时创建一个空白的699 MB文件,而下载的7 MB仅表示已填写这些空白的7 MB,但在整个下载过程中磁盘上的文件大小仍为699 MB 。

#2


1  

Angit is right, torrent client crates blank file with size = torrent_file and if you download 7MB then it fills the blank file with that and if you want to calculate first check to see if file that torrent client creates is filled with blank then you can calculate how much blanks or non blanks you have and after that is easy :).

Angit是对的,torrent客户端包装空白文件,大小= torrent_file,如果你下载7MB,那么它用空白文件填充,如果你想先计算一下,看看你的torrent客户创建的文件是否填充空白,那么你可以计算你有多少空白或非空白,之后很容易:)。

#3


1  

Make up your mind. Is your question about file sizes or the NegativeArraySizeException? File.length() gives you the length of the file as per the last time the directory was updated. If the file is still open and still being extended this information may not be current.

下定决心。您对文件大小或NegativeArraySizeException有疑问吗? File.length()为您提供上次更新目录时文件的长度。如果文件仍处于打开状态且仍在扩展,则此信息可能不是最新的。