Available hard disk space in Java

Sharky Forums


Results 1 to 10 of 10

Thread: Available hard disk space in Java

  1. #1
    Reef Shark WaRmAsTeR's Avatar
    Join Date
    Dec 2000
    Location
    Montreal, Quebec, Canada
    Posts
    451

    Available hard disk space in Java

    Hi,

    I have a program in Java that I built in order to move files from one disk to another (kind of backup) depending on the date they were created.
    Now I want to check the available disk space in a thread, and report an error (send an e-mail) if the disk space gets below a certain threshold.
    How would I determine the disk space remaining?
    Thanks!
    AMD Palomino 1700+, ECS K7S5A, ATI Radeon 8500 64MB, Seagate 7200rpm SCSI, 512mb PC2100 DDR, 2x BenQ FP783 17" LCD

  2. #2
    Expensive Sushi cjohnson's Avatar
    Join Date
    Oct 2002
    Location
    North Carolina
    Posts
    35
    I don't know of any portable/Java-ish way to do that... only thing I could think of is making Java execute 'df -h' (if you're on a UNIX) and parse that correctly... i assume there's a similar command on windows systems.
    <wik> /bin/finger that girl in the back row of machines

  3. #3
    Hammerhead Shark EverlastingGod's Avatar
    Join Date
    Feb 2003
    Location
    MD
    Posts
    1,364
    Originally posted by cjohnson
    I don't know of any portable/Java-ish way to do that... only thing I could think of is making Java execute 'df -h' (if you're on a UNIX) and parse that correctly... i assume there's a similar command on windows systems.
    Yeah, I don't think there's a pure-Java way to do it.
    Stay cool
    and be somebody's fool this year

  4. #4
    Reef Shark WaRmAsTeR's Avatar
    Join Date
    Dec 2000
    Location
    Montreal, Quebec, Canada
    Posts
    451
    My program will only be running on Windows...
    How would I do it then?
    AMD Palomino 1700+, ECS K7S5A, ATI Radeon 8500 64MB, Seagate 7200rpm SCSI, 512mb PC2100 DDR, 2x BenQ FP783 17" LCD

  5. #5
    Hammerhead Shark EverlastingGod's Avatar
    Join Date
    Feb 2003
    Location
    MD
    Posts
    1,364
    Do what cjohnson said, capture the output of a command (like dir) and parse it

    or

    Find the Windows function that checks diskspace and call it using JNI.
    Stay cool
    and be somebody's fool this year

  6. #6
    NullPointerException rock's Avatar
    Join Date
    Sep 2000
    Location
    York, PA
    Posts
    6,203
    First, there's an enhancement request for this feature here. .

    Next, the current way of doing this is allocating progressively larger amounts of space until an exception is thrown. Consider the following method (not written by me). Note that the space is allocated, but not actually written.
    Code:
    public long getDiskSpace(File path) throws IOException {
       File file = File.createTempFile("dummy","none", path);
       RandomAccessFile raf = new RandomAccessFile(file, "rw");
       
       long size = 0;
       long step = Long.MAX_VALUE - (Long.MAX_VALUE/2);
       
       while (step > 0) {
          try {
             raf.setLength(size + step);
             size += step;
          }
          catch (IOException ioe) {}
          step /= 2;
       }
       raf.close();
       file.delete();
       return size;
    }

    Open Source is free like a puppy is free.

    It's only when you look at an ant through a magnifying glass on a sunny day that you realise how often they burst into flames.

    Understanding Evolution

  7. #7
    Hammerhead Shark EverlastingGod's Avatar
    Join Date
    Feb 2003
    Location
    MD
    Posts
    1,364
    I don't know. Wouldn't allocating the space be bad even if you're not writing to it?
    Stay cool
    and be somebody's fool this year

  8. #8
    Old School OCer OS-Wiz's Avatar
    Join Date
    Dec 2001
    Location
    St. Louis, Mo, USA
    Posts
    12,242
    Originally posted by EverlastingGod
    I don't know. Wouldn't allocating the space be bad even if you're not writing to it?
    Its possible that for a very short period of time (before the file is deleted) another process could fail an allocation attempt. I'd go with a Windows command to determine space available.
    The Money Trap = Intel i7 930 | Corsair H70 | ASUS P6X58D-E | 3 x 2GB G.Skill DDR3 2000 6-9-6-24 | EVGA GTX 580 DS SC | OCZ Vertex 2 90GB SSD | WD VelociRaptor | Klipsch ProMedia | Cooler Master HAF 932 | Antec TPQ-1200W | Dell U2711 2560 x 1440 27" | Windows 7 Ultimate 64-bit | APC RS1500

  9. #9
    Hammerhead Shark EverlastingGod's Avatar
    Join Date
    Feb 2003
    Location
    MD
    Posts
    1,364
    Hehe, I was being sarcastic and rhetorical.

    Stay cool
    and be somebody's fool this year

  10. #10
    Reef Shark WaRmAsTeR's Avatar
    Join Date
    Dec 2000
    Location
    Montreal, Quebec, Canada
    Posts
    451
    Thanks a lot for the code!

    It works great except we are not sure if we are going to implement this functionnality because this is going to run on a server and we want to be sure that the disk will never be full...
    AMD Palomino 1700+, ECS K7S5A, ATI Radeon 8500 64MB, Seagate 7200rpm SCSI, 512mb PC2100 DDR, 2x BenQ FP783 17" LCD

Posting Permissions

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