• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Figuring out drive letters on WinX

 
Trailboss
Posts: 24113
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an applet that is doing the security dance to get access to all sorts of disk stuff. Now my boss says "when you are at the root directory, I want to click on .. and see a list of valid drive letters" - can such a thing be done?
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I haven't done it in an applet, but assuming the permissions work out you can use <code>File.listRoots()</code>, as in
<pre><code>
import java.io.*;
public class Rooter {
public static void main( String[] args ) {
File f[] = File.listRoots();
for (int i = 0; i < f.length; i++) {
System.out.println( f[i] );
//not f[i].getName()
}
}
}
</code></pre>
Notice that <code>getName()</code> returns an empty string in this case, so I just used <code>toString()</code> implicitly - I sense that's telling me something about the nature of a <code>File</code> object but it's too early to figure out what. More coffee...
 
paul wheaton
Trailboss
Posts: 24113
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
listRoots() is a JDK 1.2 method. The applet uses the JDK 1.1. Do you know of any 1.1 solutions?
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nope, at least no good ones. van der Linden implies that there was no way to do this prior to 1.2. Your post implies you're on Windows, so here's a hack that might get you by - note that if you don't have a disk in the floppy it won't catch that one. Put your sunglasses on, this is ugly.
<pre><code>
import java.io.*;
public class Rooter {
public static void main( String[] args ) {
File f;
char[] c = new char[1];
try {
for (c[0] = 'A'; c[0] <= 'Z'; c[0]++) {
f = new File( new String( c ) + ":\\" );
if (f.isDirectory()) {
System.out.println( f );
}
}
} catch (SecurityException e) {}
}
}
</code></pre>
 
paul wheaton
Trailboss
Posts: 24113
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I used something like that and as long as I start at 'C', it works great!
 
I don't like that guy. The tiny ad agrees with me.
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic