Real'sHowTo AddThis Feed Button
Custom Search

Get the volume labelTag(s): IO


This will retrieve the hard disk volume label :
import java.io.*;
import javax.swing.filechooser.*;

public class VolumeLabel {
 private VolumeLabel() { }

 public static void main(String[] args) {
  System.out.println("\"" + get(args[0]) + "\"");
 }

 public static String get(String path) {
  FileSystemView view = FileSystemView.getFileSystemView();
  File dir = new File(path);
  String name = view.getSystemDisplayName(dir);
  if (name == null) { return null; }
  name = name.trim();
  if (name == null || name.length() < 1) {
   return null;
  }
  int index = name.lastIndexOf(" (");
  if (index > 0) {
    name = name.substring(0, index);
   }
   return name;
 }
}
The output
Running from c:\temp

>java VolumeLabel c:
"temp"

>java VolumeLabel c:\
"HARDDISK1"

blog comments powered by Disqus


If you find this article useful, consider making a small donation
to show your support for this Web site and its content.

Written and compiled by Réal Gagnon ©1998-2014
[ home ]