Solved

Getting volume label and directory list in Java

Posted on 2002-04-07
1
338 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
1 Comment
 
LVL 3

Accepted Solution

by:
msterjev earned 50 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

914 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now