Results 1 to 8 of 8
  1. #1
    J-Live is offline Member
    Join Date
    Oct 2008
    Posts
    24
    Rep Power
    0

    Default How can you get the exact size of a file in bytes.

    So, if a file is 308.81MB, how can I get the exact size of the file in bytes? I need to get the exact size of the file because Randomaccessfile's setLength method only excepts long values. That will cause the data to become corrupt in my application. I know how to convert the 308 (multiple by 1048576 = 322961408) part but I don't know what to do with the numbers after the decimal, it comes out as 849346.56 if I multiply it by 1048576. Any ideas on what I can do? I didn't see anything that would help in the API for Randomaccessfile.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Use,

    Java Code:
    file.length();
    on the file object. It gives the size of a file in bytes. Actually it gives the size of the file content, not the disk usage of the file.

  3. #3
    J-Live is offline Member
    Join Date
    Oct 2008
    Posts
    24
    Rep Power
    0

    Default

    The file is on a server and I don't want the application to rely on just .getContentLength(). I would like to set it up so that if -1 is returned the user can input the size manually. Thank you for your response.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  5. #5
    J-Live is offline Member
    Join Date
    Oct 2008
    Posts
    24
    Rep Power
    0

    Default

    No, the files will be taken from sites like Megaupload, Deposit Files, Rapidshare, etc... They give the file size as: 90.53 MB for example. So, the user will be able to input that value. I want to convert that value to bytes.
    Last edited by J-Live; 10-27-2008 at 04:27 AM.

  6. #6
    J-Live is offline Member
    Join Date
    Oct 2008
    Posts
    24
    Rep Power
    0

    Default

    I guess I'll just round the result.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    If the file is on an HTTP server, you can use the HTTP HEAD request to get the size of the file. It will return the length in the response header field: Content-length:

Similar Threads

  1. DES algorithm (Read and Write bytes to file)
    By JoaoPe in forum Advanced Java
    Replies: 6
    Last Post: 07-29-2008, 03:46 PM
  2. [SOLVED] how to replace exact string in java
    By pankaj_salwan in forum New To Java
    Replies: 22
    Last Post: 07-08-2008, 09:28 AM
  3. File size
    By eva in forum New To Java
    Replies: 2
    Last Post: 12-19-2007, 09:27 AM
  4. Replies: 1
    Last Post: 05-20-2007, 08:52 PM
  5. Replies: 1
    Last Post: 05-19-2007, 12:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •