volume label

M

mike

Is there any way to get the volume label name in java of a certain
drive. This would be for a program that I would run in Windows XP. Of
if anyone has a suggestion of a better way to do this, I am wanting to
find out if a certain usb drive is plugged into the computer. I just
figured that the best way to test it would be to check the label,
because the label shouldn't be changing.

Any ideas?

Mike
 
T

Thomas Fritsch

mike said:
Is there any way to get the volume label name in java of a certain
drive. This would be for a program that I would run in Windows XP. Of
if anyone has a suggestion of a better way to do this, I am wanting to
find out if a certain usb drive is plugged into the computer.

You can use
File[] roots = File.listRoots();
I get {C:\, D:\, E:\} when my USB-stick is plugged in, and {C:\, D:\} when
it is plugged off.

Or you can use
boolean b = (new File("E:\\")).exists();
I get true when my USB-stick is plugged in, and false when it is plugged
off.
 
R

Roedy Green

Is there any way to get the volume label name in java of a certain
drive. This would be for a program that I would run in Windows XP. Of
if anyone has a suggestion of a better way to do this, I am wanting to
find out if a certain usb drive is plugged into the computer. I just
figured that the best way to test it would be to check the label,
because the label shouldn't be changing.

This is a platform specific notion, so you need JNI to do this. Figure
out how to do it in C and write some JNI glue. I will write it for
you for $50 US, Windows only, but of course extensible to other
platforms by writing more C code.

Other possibilities: look at some of the JNI libraries I link to at
http://mindprod.com/jgloss/jni.html Some may already have the
feature.
 
C

Chris Smith

mike said:
Is there any way to get the volume label name in java of a certain
drive.

Not precisely. However, you can get the somewhat vaguer concept of the
"system display name" of a drive. There is no specification
guaranteeing what that is, but in practice, on Windows XP, it turns out
to be the volume label, followed by the drive letter in parentheses.
Good enough?

public class Test
{
public static void main(String[] args)
{
FileSystemView v = FileSystemView.getFileSystemView();
for (File f : File.listRoots())
{
System.out.println(v.getSystemDisplayName(f));
}
}
}

Although javax.swing.filechooserFileSystemView is in the Swing package,
this use of it does not depend on any other GUI concepts. It just
happens to be that the class was placed in a Swing package mainly for
the historical reason that the people writing Swing were the first
within Sun to actually need the information that the class provides.

I've also done something odd above; normally, when you're using
FileSystemView, you'd use v.getRoots() instead of File.listRoots().
However, v.getRoots() shows the "logical" roots that should be shown to
a user, and that's "Desktop" on Windows, rather than any real filesystem
roots. If you navigated with FileSystemView, you'd have to manually
follow the path through "My Computer" to the drive, and that would
involve more OS-specific code than is really necessary.

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

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,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top