Solved

Getting volume label and directory list in Java

Posted on 2002-04-07
1
327 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
Comment Utility

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

763 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

11 Experts available now in Live!

Get 1:1 Help Now