java - How to return File[] array after adding all files to it -
I am new to this group. Please The main problem you're having Have you not started You can use Instead, you can use your Actually, what it does is to see if the file is not hidden and if its name ends with Renew Because you're doing a recursive look, you can actually add new files to your array and dynamically There are a few ways to develop it. When you can do this with plain old arrays, then In the following Then it changes the resulting list of files, so that "file" first appears and "directory" or sorted below, it is a nick, but in the list It is to ensure a certain order of files. This file list process, scanning the "file" to as an example ... file [] how to return the array like mine Code is giving
NullPointerException below, when I'm trying to add all text files from "D: \" to
files [] as an array, I I want to return the text files back in the form of a
file [] array, which will be used in the other method to read all the text files.
file [] allFiles; Int i = 0; Public file [] Search files (file source) {FileFilter filter = new FileFilter () {@Override Accept Public Bullion (file pathname) {// TODO automatically generated method stub (! Pathname.isHidden ()) {true; } return false; }}; File [] list = source.listFiles (filter); If (list! = Null) {for (file: list) {if (f.isDirectory ()) {findFiles (f); } If (f.getName (.) Contains ("txt")) {System.out.println (f.getAbsolutePath ()); Allfiles [i] = f; // It is giving nullpointerexception i ++; }}} Return all the files; // want to return it to any other method}
allFiles before using it ...
file [] list = source.listFiles (filter); If (list! = Null) {{file: list} {// ... // still all fields [I] = F; // This is nullpointerexception
allFiles = new file [list.length] , but the problem here is that you
list, as you are filtering the elements ...
FileFilter , this is the same Which is for it ... Examples ...
public file [] search files (file source) {FileFilter filter = new FileFilter () {@Override Accept public boolean (file pathname) {Return! Pathname.isHidden (); & Amp; . Pathname.getName () toLowerCase () endsWith ("txt."). }}; File [] list = source.listFiles (filter); Return list; }
.txt , for example In the form ...
list will be very easy for any type, for example ...
Uses FileFilter to search for all non-hidden files, which ends in the directory or
.txt
list and "directory", and the result
list ...
public static file [] search files (file source) {FileF Ilter filter = new FileFilter () {@Override accept public boolean (file pathname) {return! Pathname.isHidden () & amp; Amp; (Pathname.isDirectory () || pathname.getName (). ToLowerCase (). EndsWith (".txt")); }}; File [] files = source.listFiles (filter); Arrays.sort (Files, New Comparative & lt; File & gt; () {@ Compare Override Public Entry (File O1, File O2) {int compare = 0; if (o1.is directory ()) and Amp; o2.is directory ()) {compare = 0;} and if (o.is file ()) {compare = -1;} and compare {compare = 1;} return;}}); & Lt; File & gt; FileList = New Arrayist & lt; & Gt; (25); (File file: files) {if (file.isFile ()) {fileList.add (file); } And {fileList.addAll (Arrays.asList (findFiles (file))); }} Return file list. ARE (new file [fileList.size ()]); }
Comments
Post a Comment