Solved

Getting volume label and directory list in Java

Posted on 2002-04-07
1
363 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 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

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

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.

726 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