• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 222
  • Last Modified:

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?

0
jjacksn
Asked:
jjacksn
1 Solution
 
hoomanvCommented:
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
0
 
hoomanvCommented:
or you can write your own JNI to handle not-ready devices [but it doesn't worth it]
0
 
hoomanvCommented:
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)
   ....
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
hoomanvCommented:
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()));
System.out.println(in.readLine());

and it will give you: "The device is not ready."
or null instead
0
 
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.
0
 
hoomanvCommented:
I think this bug has been fixed in recent J2SE releases
the annoying message will not be prompted anymore
0
 
jjacksnAuthor Commented:
I have 1.5 installed, and it is popping up the dialog.  
0
 
hoomanvCommented:
> 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
0
 
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()

in


          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)
0
 
hoomanvCommented:
that code successfully run here without any floppy disk in drive A and no popup appeared, Im using jdk 1.5 update 1
0
 
WebstormCommented:
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
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now