Solved

How do I check available space on a drive?

Posted on 2001-06-04
8
203 Views
Last Modified: 2010-03-31
I am working on a project where I need to check if there is enough space to copy a file to a drive. Something that I can send a file size to and have it return true or false. I need a method that can look into a drive and determine if a certain number of bytes are available. Example, files from the C drive to the A drive. I can copy files over, but I need to test if there will be enough space to comlete the job.

Any direction would be helpful.
I am using JBuilder4 in a windows environment.

JDay
0
Comment
Question by:JDay
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 3

Expert Comment

by:shyamkumarreddy
ID: 6155091
Just use

java.lang.Runtime.freeMemory();
it will give u the freememory available in the system
then do arthimatic on the value.
if it is less then less space. else enough space

Cheers
Shyam
0
 
LVL 4

Expert Comment

by:rdov
ID: 6155285
Use a third party tool like JConfig that allows this type of access.
http://www.tolstoy.com/samizdat/jconfig.html
0
 

Expert Comment

by:motoras
ID: 6155979
There is no pure java solution for your problem. To find the available space on the drive you must use a native method. The java.lang.Runtime.freeMemory() returns the amount of memory disponible in JVM not on a hard drive.

0
 

Author Comment

by:JDay
ID: 6156080
Shyam,

How would you check the A: drive(floppy disk) for available space? Do you have a response from motoras's comment?

JDay
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Expert Comment

by:shyamkumarreddy
ID: 6158514
Hi JDay

I tried after the motoras has said.

The Available Space can be found using Runtime.totalMemory()

Will give ur current free space available in hard disk


Cheers
Shyam
0
 

Expert Comment

by:motoras
ID: 6159830
Dear JDay I think you have no ideea about the differnce between the available space on a hard drive and the amount of memory available for an application (in particular a JVM)
From java doc:

public long totalMemory()
Returns the total amount of memory in the Java Virtual Machine. The value returned by this method may vary over time, depending on the host environment.
Note that the amount of memory required to hold an object of any given type may be implementation-dependent.

Returns:
the total amount of memory currently available for current and future objects, measured in bytes.


0
 

Author Comment

by:JDay
ID: 6207267
Dear motoras,

I agree that it will return the java virtual machine memory.

I do have an "idea" about the difference between the different types of memeory!

Maybe Shyam doesn't.

This question still remains unsolved.

Java is so robust.
I feel like there would have to be some way to get available drive space.
JDay
0
 
LVL 1

Accepted Solution

by:
eattrig earned 200 total points
ID: 6239501
Utitize the Java Native Interface to write a wrapper class in C++ which will allow you access and give you the ability to "talk to the hardware".  Then use Windows API calls to write a C++ program that will return the total available drive space for a given drive.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now