• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to determine whether removable media is present

 
Melynda Coxx
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am writing a program in which I would like to quietly "look ahead" whether a directory exists or is available.
I am only trying to see IF a directory/file is available; I don't want to alarm my user if it is not.
The problem I am having with this is that when I run this on WindowsNT to look into the (potentially available) subdirectories on the floppy disk when there is physically disk in the drive, I get the operating system's nice dialog popup that says "There is no disk in the drive. Please insert a disk into drive A:. Abort|Retry|Ignore".
The method I am using to "peek" is
- call the static File.listRoots(), which indicates that "A:" is a valid drive.
- With my File object for the "A:\"drive, any kind of access I try
.isDirectory()
.exists()
.listFiles()
new FileInputStream(File)
etc.
gives me the offensive dialog...
I am seeking a method/alogrithm to see what's there, or whether a-drive is available, by finding a method that QUIETLY (via exception or return value) attempts access without causing the OS to pop up that irritating dialog.
Has anyone had experience with something like this? Thank you so much if you can help me out.
 
Junilu Lacar
Sheriff
Posts: 10025
120
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Melinda,
Thanks for giving fair warning in your other post about this cross-post. I guess it's only fair that I warn you that we do discourage folks from doing this The thread in JiG Intermediate will be closed.
The code below should work. I tried it on my NT machine and it ran fine. To give credit where it's due, I got most of the code from this thread (I just cleaned it up a little for you):
http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=38&t=000707

 
Melynda Coxx
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was coming back to answer my own post, because I found a solution in the meantime.
But since someone has answered in the meantime, let me first add that I did come across people who could run code like mine and the above suggestion on their NT machines without that dialog popping up, and others who got the dialog... (leads me to speculate that it might be an OS setting or version thing, I dunno...)
My case was that no matter what member method from the File class I called, I couldn't access or see-if-I-could-access without getting the dialog, including canRead() and canWrite().
An ingenius solution that someone suggested worked for me though, and I wanted to share the wealth with this group:
After I determine I am on a windows system and that A: is a root, I do a Runtime.exec() call to "cmd.exe /c dir a:", then when I try a read on the stream, the read returns null if there is no disk. (And the output from dir if there is a disk).. the empty drive rumbles for a moment, but NO DIALOG!
Resolved, and thanks.
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ooooh Melynda...that's icky!!
I think the pure-java canWrite() option is much safer and portable!!
Rob
 
Melynda Coxx
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah..., but if you will remember my initial objective, canWrite() doesn't do what I need because OS pops up the dialog...
I agree with you, Rob, that not having any system-specific stuff would be more desirable, but it actually isn't as bad in this case because the overall package that this is a part of happens to already need to determine the OS-filesystem type (Windows or Unix) in order to determine whether the 'FS root' is '/' or a collection of drive letters. AND my company's SW is supported only on specific versions of these two, so I can rightfully puke if I find anything out of place.
This thread is certainly still open to anyone who can find a (better) way to see if floppy\blah directory exists without having the Abort|Retry|Ignore dialog... I am still curious to see some of the amazing hacks that folks can dream up.
Melynda
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is an acknowledged bug in Java, which has been there for 6 years .
Sun seem incapable or unwilling to fix it. However, perhaps voting for it in the Bug Parade might persuade them to devote some time to it. It's bug number 4089199.
In the mean time, there is no pure Java solution and only a horrid Windows-specific hack can save you.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!