How to not check empty drives

I'm doing this to check for the existence of a file on all logical drives:

            File[] drives = File.listRoots();

            for (int i = 0, size = drives.length; i < size; i++) {
                  File key = new File(drives[i].getAbsolutePath()
                              + System.getProperty("file.separator") + "myfile.dat");
                  if (key.exists()) {

the problem is that i do not want the app to prompt the user to insert a disk into floppy or cd-rom drives.  Is there a state I can check to determine if a drive is empty and if so, skip it?

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

there is no built in way in java
you can execute a shell command "dir A:"
an it will give you
"The device is not ready."
its the only way I think
or you can write your own JNI to handle not-ready devices [but it doesn't worth it]
the message "The device is not ready." would be printed on stderr so you can just read a line and if it was null (because Runtime.exec do not let you read from stderr and nothing will be available through stdin either) so you know the device is not ready

Process p = Runtime.getRuntime().exec("cmd /c dir A:");
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
if(in.readLine() == null)
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

sorry I was wrong, you can read error stream as well
and also dont use "dir" simply a change drive would be enough

Process p = Runtime.getRuntime().exec("cmd /c E:");
BufferedReader in = new BufferedReader(new InputStreamReader(p.getErrorStream()));

and it will give you: "The device is not ready."
or null instead

Experts Exchange Solution brought to you by

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
Mayank SAssociate Director - Product EngineeringCommented:
BTW, you should keep these commands and error messages in a properties file because they will vary across operating systems.
I think this bug has been fixed in recent J2SE releases
the annoying message will not be prompted anymore
jjacksnAuthor Commented:
I have 1.5 installed, and it is popping up the dialog.  
> I have 1.5 installed, and it is popping up the dialog.
but I dont have such problem with 1.5
which command exactly cause the that annoying popup ?

you can use the method I've told you above to check for empty removable drives
jjacksnAuthor Commented:
I'd rather not have to do a shell exec like that if there is another way.  

I haven't actually verified where the popup is occuring, I assume its on key.exists()


          File[] drives = File.listRoots();

          for (int i = 0, size = drives.length; i < size; i++) {
               File key = new File(drives[i].getAbsolutePath()
                         + System.getProperty("file.separator") + "myfile.dat");
               if (key.exists()) {

the popup is occuring on a machine with both a floppy and cd-rom drive.  i would be it is hte floppy that is causing it.  

i will probably just not scan A since i know if is not what I am looking for (bad hack, i know)
that code successfully run here without any floppy disk in drive A and no popup appeared, Im using jdk 1.5 update 1
I also tried (no floppy disk, no CD in drives): no popup using java 1.4.2
I think it depend on OS version, I'm using Windows2000
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.