• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to Detect CD-ROM from listRoots()

 
Bibhishan Karadkar
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am getting all the roots from client ,I want to search a file on client machine ,I can skip A:\ & B:\ but how can I find CD_ROM root ,that also I want to skip , problem is many times CD_ROM drive is not fixed as E:\ ,F:\ or anything else ,how can I fing it .I can create object of this but when I getting list & length of that Object exception occurs
Is CD_ROM drive is always last ?
Can anyone help me out
Thanx ,
Bibhishan
Thanx in advance
 
prabhat gupta
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bibhishan,
Well, it is very easy.
Try the following code.It will work fine.
import java.io.*;
class FileRoots
{
FileRoots()
{
try{
File f=new File(".");
File[] roots=f.listRoots();
for(int i=0;i<roots.length;i++)>
{
if(roots[i].canRead()==true)
{System.out.println("drive: "+roots[i].toString());
//do here whatever you want to do from this root drive.
else
System.out.println("cannot read from drive: "+roots[i].toString());
}
catch(Exception e){e.printStackTrace();}
}end of constructor
public static void main(String args[])
{
new FileRoots();
}//end of main
}//end of class
 
prabhat gupta
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey i forgot to tell you something.
If you dont have your CD rom drive blank ie with no CD inside for the system to read then the code will enter the else part of the if statement .It will be better if you use the canWrite() method instead of canRead(),since the code may not work fine with canRead() method if the cd rom is in the drive.Hence I suggest that you replace canRead() with canWrite().
 
Bibhishan Karadkar
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx Prabhat
I got this ,thanx for U'r reply
yes I will use canWrite() method it will work
By for Now ,
Bibhishan
 
Martin Thorpe
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello I have been trying something similar.

I have already got as far as you with the use of canRead() (i want to check the CD drives so am using read not write).

But I still have one problem: on windows2000 JDK 1.4 is that when it looks to the A:/ drive it displays a warning alert box:
"no disk in drive"
Try Again, Cancel, Continue

All I want is to return a list of the drives at this point not actually access them? Any ideas how I could get around this other than excluding A: and B: drives.

cheers
Martin
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!