0

file.listRoots() works fine for internal file drive information, but is not getting USB and portable file info. Here is my code:

File[] paths; 
try {
    // returns pathnames for files and directory
    paths = File.listRoots();
    for (File path : paths) // for each pathname in pathname array
        System.out.println(path); // prints file and directory paths
} catch(Exception e) { // if any error occurs
    e.printStackTrace();
}

I don't understand why does my portable USB info not fetch?

0009laH
  • 1,960
  • 13
  • 27
Harish Rawal
  • 226
  • 2
  • 15

1 Answers1

2

If your USB drive is connected and is accessible, you could check whether NIO code shows the volumes as follows:

for (Path root : FileSystems.getDefault().getRootDirectories()) {
    FileStore fs = Files.getFileStore(root);
    System.out.format("FileStore %s\tName: '%s'\tType: %s%n", root, fs.name(), fs.type());
    System.out.println();
}

Inside above loop you can also check other filesystem attributes such as removable storage flag:

String[] attrs = new String[]{"volume:isRemovable"};

for (String s : attrs) {
    System.out.format("\t%s=%s", s, fs.getAttribute(s));
}
DuncG
  • 12,137
  • 2
  • 21
  • 33
  • i have used this but again only show C, D, E drive not my connected usb device – Harish Rawal Sep 30 '20 at 11:26
  • Is your drive is assigned or mapped with a drive letter in Windows Explorer? If not, then this code won't help. – DuncG Sep 30 '20 at 11:42
  • my device drive name is moto – Harish Rawal Sep 30 '20 at 11:51
  • what is the solution? – Harish Rawal Sep 30 '20 at 11:52
  • Some phones / tablets expose their discs so that Windows drive mapping works - and you would be able to read/write via Java easily. However it sounds like your device may use Media Transfer Protocol and you'd need to use MTP aware client (Windows Explorer is one such app). This topic has been mentioned here [https://stackoverflow.com/questions/39210514/access-mtp-device-with-java] – DuncG Sep 30 '20 at 13:00