How can i get all the list of directories and files in a drive in java. (All files in C:\) -


i writing gui program java fx. user can choose directory in system. unfortunately, directory chooser let user choose drive too. can list out files , folders in directory using file.listfiles(). happens if user choose drive. listfiles() failing there null pointer exception.

is there way can list of files , directories in drive in java?

    //get files user computer     public void getfilenames(file folder) {         (final file file : folder.listfiles()) {             if (file.isdirectory()) {                 getfilenames(file);             } else {                 if (filenameutils.isextension(file.getname().tolowercase(), videoformatset)) {                     //don't consider video files less 100 mb                     final long filesizeinmb = file.length() / 1048576;                     if (filesizeinmb < 100) {                         continue;                     }                     final string filename = filenameutils.removeextension(file.getname());                     if (!movienamefilter(filename).isempty()) {                         moviefilenamelist.add(movienamefilter(filename));                     } else {                         moviefilenamelist.add(filename);                     }                 } 

edit:

the code lets user chooses directory (folder variable). getfilenames() extract movie file name in chosen directory. code working fine when user selecting directory.

but,javafx lets user select drive (i using directory chooser). lets example, chooses c drive (c:) in system.

the value of file folder = e:\

the null pointer exception happening when doing folder.listfiles() in 171st line

    171: (final file file : folder.listfiles()) { 

i can avoid adding validation not letting user chose drive.

but if want let him choose drive. there way can list of files in drive?

simply put, possible write code lets user provide drive (ex: c or d) can display files in drive.

stack trace:

exception in thread "thread-5" java.lang.nullpointerexception @ com.imdbrater.application.javafxapplication$getfilesthread.getfilenames(javafxapplication.java:171) @ com.imdbrater.application.javafxapplication$getfilesthread.getfilenames(javafxapplication.java:173) @ com.imdbrater.application.javafxapplication$getfilesthread.getfilenames(javafxapplication.java:173) @ com.imdbrater.application.javafxapplication$getfilesthread.run(javafxapplication.java:167) @ java.lang.thread.run(unknown source)

i noticed performed calls different movienamefilter() method, not declared in piece of code posted. please make sure method returning correct value, call methods on returning objects.

you didn't post nullpointerexception happening, base answer on simple , possible null output on operation trying perform. please sure drive input user exists. can seen in the api documentation java.io.file,

returns:

an array of abstract pathnames denoting files , directories in directory denoted abstract pathname. array empty if directory empty. returns null if abstract pathname not denote directory, or if i/o error occurs.

in case, if user inputs path not exist, npe.

the best approach handle firstly validate user input. secondary step involves validating output of listfiles() call.

// files user computer public void getfilenames(file folder) {      // retrieve file listing     file[] filelist = folder.listfiles();      if (filelist == null) {         // throw exception, return or other error handling here         return;     }      // path correct     (final file file : filelist ) {         // etc etc etc     } } 

if npe related other error, please comment may update answer accordingly.


note: please remember filepaths in java escaped double backslashes, should make call in way such follows.

file rootfolder = new file("c:\\"); getfilenames(rootfolder);  file downloadsfolder = new file("c:\\users\\rahul\\downloads"); getfilenames(downloadsfolder); 

Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -