How to list drives on a user's computer?

W

Will Clark

Using File.list() I can get a String[] of the available files/directories in
a certain directory specified by the File object. However, I was wondering
how to get a list of the available drives?... (PC/Mac - not Unix)

Cheers :eek:)

Will
 
M

Michael Borgwardt

Will said:
Using File.list() I can get a String[] of the available files/directories in
a certain directory specified by the File object. However, I was wondering
how to get a list of the available drives?... (PC/Mac - not Unix)

File.listRoots()
 
W

Will Clark

Cheers! Missed that one :eek:)


Michael Borgwardt said:
Will said:
Using File.list() I can get a String[] of the available files/directories in
a certain directory specified by the File object. However, I was wondering
how to get a list of the available drives?... (PC/Mac - not Unix)

File.listRoots()
 
W

Will Clark

Ah, yes, but is there any way of doing it in Java 1.1? (Sorry to be a pest,
but I need it to work on old JavaVMs as a compatibility thing...)

Cheers :eek:)

Roedy Green said:
Using File.list() I can get a String[] of the available files/directories in
a certain directory specified by the File object. However, I was wondering
how to get a list of the available drives?.

see http://mindprod.com/jgloss/file.html

Hint. they are generically called "roots".
 
C

Chris Smith

Will said:
Ah, yes, but is there any way of doing it in Java 1.1? (Sorry to be a pest,
but I need it to work on old JavaVMs as a compatibility thing...)

No, there isn't. Of course, if you know that you're working with
Windows, you can just cycle through the alphabet and use File.exists()
to find out which drives are present and which aren't.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
R

Roedy Green

You will need some JNI to do the same thing listRoots does.

Another way is to exec some native utility that tells you what you
need to know.

The technique of seeing if you can create a temp file on every
possibility will give you a pretty good idea. That would exclude CD
readers, but could write on a CD with CDDirect.
 
W

Will Clark

Unfortunately, it needs to work on a Mac too... and they don't have drive
letters (shame!)

Roedy has mentioned JNI... I'll have a look into that, but it may be easier
to get people to upgrade to Java 1.2!

Cheers tho' :eek:)

Will
 
R

Roedy Green

Roedy has mentioned JNI... I'll have a look into that, but it may be easier
to get people to upgrade to Java 1.2!

the problem with the JNI method is you have to implement it separately
with each platform and create different distributions, and worst of
all debug them all separately.
 
P

Phil...

sounds like you are just pulling our chain
you could let the user type it in to main as args
 
W

Will Clark

Roedy has mentioned JNI... I'll have a look into that, but it may be
easier
the problem with the JNI method is you have to implement it separately
with each platform and create different distributions, and worst of
all debug them all separately.

Yeah, hence why it might just be easier to get people to upgrade to 1.2! But
I'm going to look into how the JDK 1.2 source does it... because as Chris
Smith pointed out (and it's how the JDK does it) it is possible to just
cycle through the alphabet and use File.exists() - well actually
System.getSecurityManager().checkRead() as well

--->o---- (blatant rip from Win32FileSystem.java) ----

private boolean access(String path) {
try {
SecurityManager security = System.getSecurityManager();
if (security != null) security.checkRead(path);
return true;
} catch (SecurityException x) {
return false;
}
}

private static native int listRoots0();

public File[] listRoots() {
int ds = listRoots0();
int n = 0;
for (int i = 0; i < 26; i++) {
if (((ds >> i) & 1) != 0) {
if (!access((char)('A' + i) + ":" + slash))
ds &= ~(1 << i);
else
n++;
}
}
File[] fs = new File[n];
int j = 0;
char slash = this.slash;
for (int i = 0; i < 26; i++) {
if (((ds >> i) & 1) != 0)
fs[j++] = new File((char)('A' + i) + ":" + slash);
}
return fs;
}
 
W

Will Clark

sounds like you are just pulling our chain
you could let the user type it in to main as args

Unfortunately that approach won't work in this situation.

Cheers for the suggestion tho' :eek:)
 
P

Phil...

This just occurred to me. Use File.getCanonicalPath(".");
to get a string that starts with the drive designation. If it
is '/' then you are most likely on Unix/Linux, but can check
by testing for "/usr". If it is a letter with colon, you are most
likely on Windoze and you can check through the alphabet.
If it is something else, it might be Mac OS9 or whatever
(I don't know what they use.) For Mac OSX it might
also be '/'.

Will Clark said:
the problem with the JNI method is you have to implement it separately
with each platform and create different distributions, and worst of
all debug them all separately.

Yeah, hence why it might just be easier to get people to upgrade to 1.2! But
I'm going to look into how the JDK 1.2 source does it... because as Chris
Smith pointed out (and it's how the JDK does it) it is possible to just
cycle through the alphabet and use File.exists() - well actually
System.getSecurityManager().checkRead() as well

--->o---- (blatant rip from Win32FileSystem.java) ----

private boolean access(String path) {
try {
SecurityManager security = System.getSecurityManager();
if (security != null) security.checkRead(path);
return true;
} catch (SecurityException x) {
return false;
}
}

private static native int listRoots0();

public File[] listRoots() {
int ds = listRoots0();
int n = 0;
for (int i = 0; i < 26; i++) {
if (((ds >> i) & 1) != 0) {
if (!access((char)('A' + i) + ":" + slash))
ds &= ~(1 << i);
else
n++;
}
}
File[] fs = new File[n];
int j = 0;
char slash = this.slash;
for (int i = 0; i < 26; i++) {
if (((ds >> i) & 1) != 0)
fs[j++] = new File((char)('A' + i) + ":" + slash);
}
return fs;
}
 
R

Roedy Green

This just occurred to me. Use File.getCanonicalPath(".");
to get a string that starts with the drive designation. If it
is '/' then you are most likely on Unix/Linux, but can check
by testing for "/usr". If it is a letter with colon, you are most
likely on Windoze and you can check through the alphabet.
If it is something else, it might be Mac OS9 or whatever
(I don't know what they use.) For Mac OSX it might
also be '/'.

More directly just ask the system properties.

see http://mindprod.com/wassup.html
 
W

Will Clark

Yes, for MacOS X it is '/'... and the available volumes are at '/Volumes/'

Unfortunately, Apple decided not to release Java 1.2 for MacOS 9 and lower,
so I can't try any hack through their classes to find out how to list the
roots on pre-OSX machines

And (new File(".")).getCanonicalPath() just brings up a security exception!
(Applet restrictions which, for some reason or other, cannot be bypassed,
even when signed)

Back to the drawing board :eek:)
 
Joined
Feb 8, 2009
Messages
1
Reaction score
0
how do i list devices logical name?

hello i want to make the same thing as in jfilechooser:
devices appear in popup menu with their logical name like g:/MyBook not only g:/
thank you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top