Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (494)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Getting the capacity of a disk  (Read 1597 times)
0 Members and 1 Guest are viewing this topic.
kul_th_las
Guest
« Posted 2005-01-21 01:47:51 »

What I want to do is programatically get either:
(a) the capacity of a disk, such as a hard drive or
(b) the remaining space - preferably in bytes, but I'm flexible.

I'll settle for just one of these, or a pointer to the API that allows this.

Any ideas?
Offline Ceranith

Senior Newbie





« Reply #1 - Posted 2005-01-21 02:01:01 »

You can't do this with Java yet. I found the RFE for it though:

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4057701

It says it's targeted for 1.6. So until then, there's not much you can do unless you use a native call.
Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #2 - Posted 2005-01-21 05:58:17 »

Quote

It says it's targeted for 1.6. So until then, there's not much you can do unless you use a native call.


Many of us have been waiting since 1.1 for this, so don't hold your breath - expect it to be delayed until java 10 or something.

I mean, knowing whether you can move a file without corrupting it, and whether you can make a new file, and stuff like that, aren't important are they? No-one ever writes a server that is intelligent enough not to crash the OS by taking up too much space with it's logfiles, do they? Nor puts any defensive code in their clients?
</bitter, bitter sarcasm>

malloc will be first against the wall when the revolution comes...
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline aNt

Senior Member




AFK


« Reply #3 - Posted 2005-01-21 06:53:15 »

this only works on windows. its a hack and yes i agree
with bla on this one also. bit of an on going joke.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
public static final long getFreeSpace(File file) throws IllegalArgumentException, IllegalStateException, IOException {
            if (file == null) throw new IllegalArgumentException("file == null");
 
            if (System.getProperty("os.name").startsWith("Windows"))
                  return getFreeSpaceInWindows(file);
            //else if (System.getProperty("os.name").startsWith("..."))
           //      return ...
           else
                  throw new IllegalStateException("getFreeSpace(File) currently does not support this operating system: " + System.getProperty("os.name"));
      }
 
 
      private static final long getFreeSpaceInWindows(File file) throws IllegalStateException, IOException {
            PrintWriter writer = null;
            BufferedReader reader = null;
            try {
                        // create a temp .bat file to run the dir command:
                 File script = File.createTempFile("script", ".bat");
                  script.deleteOnExit();
                  writer = new PrintWriter( new FileWriter(script, false), true );
                  writer.println("dir \"" + file.getCanonicalPath() + "\"");
                  writer.close();      // MUST close it at this point, else the Process below will fail to run
                 
                        // get output from running the .bat file:
                 Process p = Runtime.getRuntime().exec( script.getAbsolutePath() );
                  reader = new BufferedReader( new InputStreamReader( p.getInputStream() ) );
                  String line = null;
                  while (true) {
                        line = reader.readLine();
                        if (line == null) throw new IllegalStateException("Failed to encounter the expected output (a line ending with the text \"free\") while parsing the output of the dir command");
                        else if (line.endsWith("free")) break;
                  }
                  // Here are some real examples seen of what line can look like:
                 //        12 dir(s)     631,889,920 bytes free
                 //        10 dir(s)     24,167.73 MB free
                 //                      788,021,248 bytes free
                 // (This last case happens if there are no contents inside volume)
                 // The parsing code below MUST handle all these cases

                        // parse the number (of bytes free) from line:
                 String[] tokens = line.split("\\s+", -1);      // in ALL cases, should have tokens[tokens.length - 3] = "788,021,248", tokens[tokens.length - 2] = "bytes" or "KB" or "MB" or "GB", tokens[tokens.length - 1] = "free"
                 String numberText = tokens[tokens.length - 3].replaceAll(",", "");      // replaceAll eliminates the commas but NOT any decimal point
                 String unit = tokens[tokens.length - 2];
                  if (unit.equals("bytes"))
                        return Long.parseLong(numberText);
                  else {
 
                        System.err.println("--------------------------------------------------");
                        System.err.println("WARNING: POSSIBLE LOSS OF PRECISION in determining the free space");
                        System.err.println("The operating system reported the free space as " + numberText + " " + unit);
                        System.err.println("This output may have been rounded, however, which means that the exact number of bytes is impossible to determine");
                        System.err.println("--------------------------------------------------");
                        double number = Double.parseDouble(numberText);
                        if (unit.equals("KB")) return (long) (number * 1024);      // see       http://groups.google.com/groups?dq=&hl=en&lr=&ie=UTF-8&threadm=QNJ6c.54315%24H44.994342%40bgtnsc04-news.ops.worldnet.att.net&prev=/groups%3Fhl%3Den%26lr%3D%26ie%3DUTF-8%26group%3Dcomp.os.msdos.programmer
                       else if (unit.equals("MB")) return (long) (number * 1024 * 1024);
                        else if (unit.equals("GB")) return (long) (number * 1024 * 1024 * 1024);
                        else throw new IllegalStateException("Program encountered unit = " + unit + " which it is unable to handle");
                  }
            }
            finally {
                  if (writer != null) writer.close();
                  if (reader != null) reader.close();
            }
      }
Offline aNt

Senior Member




AFK


« Reply #4 - Posted 2005-01-21 06:54:49 »

oh to note- u could do a .sh script on macosx i guess.
but then under mac i think theres a java call that can
give u this info if u use there os java additions.
Offline Jackal von ÖRF

Junior Member





« Reply #5 - Posted 2005-01-21 12:53:19 »

Quote
this only works on windows. its a hack and yes i agree
with bla on this one also. bit of an on going joke.

This code works fine on Linux. On Windows the exec("dir") will fail. Since the latest Mac OS is UNIX-based, a similar code should work well on it. So we can conclude that Windows sucks. Grin
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
            Process p;
            if (System.getProperty("os.name").startsWith("Windows")) {
                  p = Runtime.getRuntime().exec("dir");
            } else if (System.getProperty("os.name").startsWith("Linux")) {
                  p = Runtime.getRuntime().exec("df");
            } else {
                  return;
            }
           
            BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
           
            String line;
            while ((line = r.readLine()) != null) {
                  System.out.println(line);
            }

Offline nlmueng

Junior Member





« Reply #6 - Posted 2005-01-21 17:07:21 »

You need to do:

p = Runtime.getRuntime().exec("cmd.exe /c dir");

for windows

kul_th_las
Guest
« Reply #7 - Posted 2005-01-22 03:59:52 »

Well, that's too bad. I was hoping for a platform independent solution. I'm writing a quick app that will helps you manage your disk space.

The idea is to solve this problem: Say you want to add more music to your digital music library, and you need about 1.5 GB - but you've only got 700 MB left on your hard disk. Or maybe you want to install that new game and need to get rid of something before it will fit. So, it's time to clean out the old hard disk, but what to delete/uninstall? Well this program would visually allow you to see how your space is being used, and what files/directories are taking up more than their fair share of space, allowing to you make a quick, informed decision.

I can live without the "get disk capacity" Java feature, but it sure would be nice. It's gotta run on at least Win and Mac -  without any hack code. But I do appreciate the answers.
Online tom
« Reply #8 - Posted 2005-01-22 11:19:49 »

Have you checked out jDiskReport?

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 793
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #9 - Posted 2005-01-31 06:26:26 »

Platform independant and _pretty accurate_.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
   public static final long getFreeDiskSpaceAtRoot(File root)
   {
      try
      {
         // CREATE FILE AS BIG AS POSSIBLE

         final File file = new File(root.getAbsolutePath() + "big_temp_file_should_have_been_deleted.txt");

         final RandomAccessFile raf = new RandomAccessFile(file, "rw");

         long size = 0;
         long step = Long.MAX_VALUE / 2;

         while (step > 0)
         {
            try
            {
               raf.setLength(size + step);

               size += step;
            }
            catch (java.io.IOException ioe)
            {
               step /= 2;
            }
         }

         raf.close();

         // DELETE FILE

         file.delete();

         return size;
      }
      catch (Exception exc)
      {
         exc.printStackTrace();

         return -1;
      }
   }


No need to wait for 1.6 Grin

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Dwinin (19 views)
2014-09-12 09:08:26

Norakomi (54 views)
2014-09-10 13:57:51

TehJavaDev (63 views)
2014-09-10 06:39:09

Tekkerue (31 views)
2014-09-09 02:24:56

mitcheeb (53 views)
2014-09-08 06:06:29

BurntPizza (37 views)
2014-09-07 01:13:42

Longarmx (23 views)
2014-09-07 01:12:14

Longarmx (27 views)
2014-09-07 01:11:22

Longarmx (27 views)
2014-09-07 01:10:19

mitcheeb (35 views)
2014-09-04 23:08:59
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!