Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

In java, you can use File.listRoots() to get all drives in the system.

I'm looking to get only the external drives, i.e. USB drives, external hard disks, optical drives, floppy, etc.

Is there any way to do it in java? If not, native C++ code would be good as well. In that case, I need both Windows and Linux code.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
334 views
Welcome To Ask or Share your Answers For Others

1 Answer

To get file system info, use something like:

import java.io.*;
import javax.swing.filechooser.*;

public class DriveTypeInfo
{
  public static void main(String[] args)
  {
      System.out.println("File system roots returned by   FileSystemView.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());
      }
   }
}

Also see this, this and this question.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...