How do I check available space on a drive?

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.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Just use

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

Use a third party tool like JConfig that allows this type of access.
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.

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

JDayAuthor Commented:

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

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

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.

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

JDayAuthor Commented:
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.
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.

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.