How to create a file that points to a disk drive with no disk.
I could not find my last post about this and so I'm asking again Is there any way to by-pass the windows error
"No Disk" "Exception processing message etc. etc
when I say File disk = new File("A:/")?
Here is my code;
privatevoid createDriveList(){
List tempList = Collections.synchronizedList(new ArrayList());
driveList.add("A:/");
driveList.add("B:/");
driveList.add("C:/");
driveList.add("D:/");
driveList.add("E:/");
driveList.add("F:/");
driveList.add("G:/");
driveList.add("H:/");
driveList.add("I:/");
driveList.add("J:/");
driveList.add("K:/");
driveList.add("L:/");
driveList.add("M:/");
driveList.add("N:/");
driveList.add("O:/");
driveList.add("P:/");
driveList.add("Q:/");
driveList.add("R:/");
driveList.add("S:/");
driveList.add("T:/");
driveList.add("U:/");
driveList.add("V:/");
driveList.add("W:/");
driveList.add("X:/");
driveList.add("Y:/");
driveList.add("Z:/");
try{
for(int d=0;d<driveList.size();d++){
String drive=(String)driveList.get(d);
File Drive =new File(drive);
if(Drive.exists()){
System.out.println("Drive "+drive+" exists");
if(Drive.isDirectory()&&Drive.canRead())tempList.add(drive);
}
}
driveList= Collections.synchronizedList(new ArrayList());
driveList=tempList;
}catch(Exception x){System.out.println(x.getMessage());}
}
>
Use Runtime.exec() method with an OS command like "dir" and interact with the OS by using I/O streams the returned Process object has. Java API generally doesn't have facilities to do direct interaction with the low-level OS.
hiwaa at 2007-7-12 18:34:04 >

> Is there any way to by-pass the windows error
> "No Disk" "Exception processing message etc. etc
> when I say File disk = new File("A:/")?
I don't get this error with Java 1.6 and Windows XP SP2. Mind you I had to add some other stuff to get your code to compile. I get this output:Drive C:/ exists
Drive Y:/ exists
Drive Z:/ exists
Perhaps you could post a small compilable example which shows the problem - compilable in the sense that it has a main() method, and declares (and initialises) driveListetc. Small as in it doesn't need all those drives if "A:/" is giving an error and if the error occurs with new File("A:/") then most of the code in createDriveList() could be snipped.
Post the code, and say what behaviour you see (exact error messages and their stack trace).
Use Runtime.exec() method with an OS command like "dir" and interact with the OS by using I/O streams the returned Process object has. I'm not sure I understand how to do this could you give me an example of what you mean?-RC
> > Is there any way to by-pass the windows error
> > "No Disk" "Exception processing message etc. etc
> > when I say File disk = new File("A:/")?
>
> I don't get this error with Java 1.6 and Windows XP
> SP2.
Nor do I with 1.5 and XP/SP2.
jverda at 2007-7-12 18:34:04 >

> Use Runtime.exec() method with an OS command like
> "dir" and interact with the OS by using I/O streams
> the returned Process object has.
> I'm not sure I understand how to do this could you
> give me an example of what you mean?
> -RC
That's rather kludgey. I'd only do that as a last ditch approach. Follow the other poster's advice first to see if you can figure out what's going on.
jverda at 2007-7-12 18:34:04 >

driveList= Collections.synchronizedList(new ArrayList());
driveList=tempList;
Also, the first line might as well not even be there. You create a new ArrayList, and then you just throw it away. The second line doesn't copy anything from one object to another. All it does is cause the driveLits reference variable to point to the same object as the tempList variable is pointing to.
jverda at 2007-7-12 18:34:04 >

Reg,
You're in luck... this exact "input | exec | output" question came up on the forums a while back...
package forums;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class ProcessBuilderExample {
public static void main(String[] args) {
BufferedReader br = null;
try {
ProcessBuilder processBuilder = new ProcessBuilder("C:\\WINDOWS\\system32\\systeminfo");
Process process = processBuilder.start();
br = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
if (line.matches(".*Total Physical Memory.*")) {
System.out.println(line);
}
}
} catch(Exception e){
e.printStackTrace();
} finally {
try{br.close();}catch(Exception ignore){}
}
}
}
I dont want driveList to contain any String that does not actually name a disk on my machine.
So the for loop checks driveList to see if file exists and if it does it adds it to tempList.
Then I copy tempList to my private variable driveList which is used elsewhere in the program.
Before I copy to driveList I empty it by re-initializing it.
Is this not kosher for some reason?
It works I've used this method many times with no problem.
> I dont want driveList to contain any String that does
> not actually name a disk on my machine.
> So the for loop checks driveList to see if file
> exists and if it does it adds it to tempList.
> Then I copy tempList to my private variable driveList
> which is used elsewhere in the program.
> Before I copy to driveList I empty it by
> re-initializing it.
> Is this not kosher for some reason?
> It works I've used this method many times with no
> problem.
I'm not following what you're saying. But again...
driveList = Collections.synchronizedList(new ArrayList());
driveList = tempList;
the first line is pointless. It accomplishes nothing. It creates a new ArrayList that's never used and then lets it float away. Your code will behave exactly the same if that first line is not even there.
jverda at 2007-7-12 18:34:04 >

> Then I copy tempList to my private variable driveList
You mean driveList = tempList? That doesn't copy any List contents. It just means that the driveList variable now points to the same List object as what the tempList variable points to.
Variables don't hold objects. They hold references.
jverda at 2007-7-12 18:34:04 >

> Before I copy to driveList I empty it by
> re-initializing it.
You mean this? driveList = Collections.synchronizedList(new ArrayList());
That doesn't empty any list. It just creates a new list. It doesn't to anything to the List that driveList was previously pointing to.
jverda at 2007-7-12 18:34:04 >

Yes you are right I could blow away the first line.
That was just a side observation, however. Why don't you do what was suggested in reply 2?Also, what version of Windows and what version of Java are you using?
jverda at 2007-7-12 18:34:04 >

@ OP. Doesn't File.listRoots() do what you want http://java.sun.com/javase/6/docs/api/
> @ OP. Doesn't File.listRoots() do what you want> > http://java.sun.com/javase/6/docs/api/Excellent point.
jverda at 2007-7-21 22:09:50 >

I'm usingWindows XP Professional SP2and Oracle JDeveloper 10g with the jdk-1_5_0 version.
> Doesn't File.listRoots() do what you want?A really excellent point.
File.listRoots is the answer.Thank you so very much.
My code is now much cleaner :
private void createDriveList(){
List tempList = Collections.synchronizedList(new ArrayList());
File[] roots=File.listRoots();
for(int r=0;r<roots.length;r++){
System.out.println("Roots = "+roots[r]);
tempList.add(roots[r]);
}
try{
for(int d=0;d<tempList.size();d++){
File Drive=(File)tempList.get(d);
driveList.add(Drive.getAbsolutePath());
}
}catch(Exception x){System.out.println(x.getMessage());}
}
Thanks to all for your time and efforts I am much obliged to you all.
Reg.">