i tried the following program
import java.io.*;
class dr
{
public static void main(String args[])
{
try{
File[] roots = File.listRoots();
for (int index = 0; index < roots.length; index++)
{ //Print out each drive/partition
System.out.println(roots[index].toString());
}
}
catch(Exception e)
{
System.out.println("error " +e);
}
}
}
but in my system floppy drive is not connected
and i am getting a message like the following
" The drive is not ready for use;its door may be open,Please check drive A: and make sure that disk is inserted and that the drive door is closed"
then three options are shown cancel,try again,continue
when i try continue,it works
but how i could avoid that msg
What are you trying to do?
My recommendation would be to use FileSystemView.
It's used something like this:
FileSystemView fsv = FileSystemView.getFileSystemView();
File[] roots = fsv.getRoots();
for (File f: roots) {
System.out.println(fsv.getSystemDisplayName(f);
}
package com.littletutorials.fs;
import java.io.*;
import javax.swing.filechooser.*;
public class DriveTypeInfo
{
public static void main(String[] args)
{
System.out.println("File system roots returned byFileSystemView.getFileSystemView():");
FileSystemView fsv = FileSystemView.getFileSystemView();
File[] roots = fsv.getRoots();
for (int i = 0; i < roots.length; i++)
{
System.out.println("Root: " + roots[i]);
}
System.out.println("Home directory: " + fsv.getHomeDirectory());
System.out.println("File system roots returned by File.listRoots():");
File[] f = File.listRoots();
for (int i = 0; i < f.length; i++)
{
System.out.println("Drive: " + f[i]);
System.out.println("Display name: " + fsv.getSystemDisplayName(f[i]));
System.out.println("Is drive: " + fsv.isDrive(f[i]));
System.out.println("Is floppy: " + fsv.isFloppyDrive(f[i]));
System.out.println("Readable: " + f[i].canRead());
System.out.println("Writable: " + f[i].canWrite());
System.out.println("Total space: " + f[i].getTotalSpace());
System.out.println("Usable space: " + f[i].getUsableSpace());
}
}
}
reference : http://littletutorials.com/2008/03/10/getting-file-system-details-in-java/
When it comes to Windows, this class WindowsAltFileSystemView
proposes an alternative based on FileSystemView
This class is necessary due to an annoying bug on Windows NT where instantiating a JFileChooser
with the default FileSystemView
will cause a "drive A: not ready
" error every time.
I grabbed the Windows FileSystemView
impl from the 1.3 SDK and modified it so * as to not use java.io.File.listRoots()
to get fileSystem roots.
java.io.File.listRoots()
does a SecurityManager.checkRead()
which causes the OS to try to access drive A:
even when there is no disk, causing an annoying "abort, retry, ignore
" popup message every time we instantiate a JFileChooser
!
So here, the idea is to extends FileSystemView
, replacing the getRoots()
method with:
/**
* Returns all root partitians on this system. On Windows, this
* will be the A: through Z: drives.
*/
public File[] getRoots() {
Vector rootsVector = new Vector();
// Create the A: drive whether it is mounted or not
FileSystemRoot floppy = new FileSystemRoot("A" + ":" + "\\");
rootsVector.addElement(floppy);
// Run through all possible mount points and check
// for their existance.
for (char c = 'C'; c <= 'Z'; c++) {
char device[] = {c, ':', '\\'};
String deviceName = new String(device);
File deviceFile = new FileSystemRoot(deviceName);
if (deviceFile != null && deviceFile.exists()) {
rootsVector.addElement(deviceFile);
}
}
File[] roots = new File[rootsVector.size()];
rootsVector.copyInto(roots);
return roots;
}
you can use this;
import java.io.File;
class dr
{
public static void main(String args[])
{
File[] roots=File.listRoots();
for(File root:roots)
{
System.out.println(root.getName());
}
}
}