?
Solved

Getting volume label and directory list in Java

Posted on 2002-04-07
1
Medium Priority
?
370 Views
Last Modified: 2007-12-19
I need to write a program to capture a volume label of a CD-ROM drive and also a list of all the folders and root files on the CD. I'd like to use Java to do this.

Can anyone point me to any resources which give info on this?
0
Comment
Question by:almond_uk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 3

Accepted Solution

by:
msterjev earned 200 total points
ID: 6923921

Windows solution for volume labels:

Kernel32.java


import java.util.*;

public class Kernel32
{
    static
    {
         System.loadLibrary("JKernel32");
    }
   
    private static native String getLogicalDriveStrings();
    public static native String getVolumeLabel(String root);
   
    public static Enumeration enumLogicalDrives()
    {
         Vector v=new Vector();
         String drives=getLogicalDriveStrings();
         StringTokenizer st=new StringTokenizer(drives);
         while(st.hasMoreTokens())
              v.addElement(st.nextToken());
         return v.elements();
    }
}


Generate header with javah:

javah Kernel32

Start Visual C++ and make new dll with the name JKernel32.
Import here previously generate Kernel32.h.

Insert new C++ file and type here:

#include "Kernel32.h"
#include <windows.h>

JNIEXPORT jstring JNICALL Java_Kernel32_getLogicalDriveStrings(JNIEnv * env, jclass c)
{
    char buffer[256]={0};
    char drives[256]={0};
    ::GetLogicalDriveStrings(256,buffer);
    char *p=buffer;
    lstrcpy(drives,"");
    while(*p!=0)
    {
         strncat(drives,p,3);
         strcat(drives," ");
         p+=4;
    }
    return
         env->NewStringUTF(drives);
}


JNIEXPORT jstring JNICALL Java_Kernel32_getVolumeLabel(JNIEnv * env, jclass c, jstring r)
{
    const char *root=env->GetStringUTFChars(r,0);
    char volumeNameBuffer[MAX_PATH];
   DWORD lVolumeSerialNumber=0;
    DWORD lMaximumComponentLength=0;
    DWORD lFileSystemFlags=0;
    char fileSystemNameBuffer[10];
    if(::GetVolumeInformation(root,volumeNameBuffer,MAX_PATH,&lVolumeSerialNumber,&lMaximumComponentLength,&lFileSystemFlags,fileSystemNameBuffer,10))
    {
         env->ReleaseStringUTFChars(r,root);
         return env->NewStringUTF(volumeNameBuffer);
    }
    env->ReleaseStringUTFChars(r,root);
    return NULL;
}


Compile the project and put  JKernel32.dll into the same directory as Kernel32 class.

Try this:

Test.java

import java.util.*;

public class Test
{
    public static void main(String[] args)
    {
         String drive,label;
         Enumeration e=Kernel32.enumLogicalDrives();
         while(e.hasMoreElements())
         {
              drive=(String)e.nextElement();
              label=Kernel32.getVolumeLabel(drive);
              System.out.println(drive+":"+label);
         }
    }
}

After capturing the roots you use File Object and its methods.
Enjoy.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month11 days, 12 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question