Solved

Getting volume label and directory list in Java

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

695 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