List of all files from the entire folder in one list

Hi, I am looking for a solution to list all files from a root / Android device.

Suppose there are 3 folders in the root directory, but I want to display all the files in this entire folder in one list.

Now if i use

File f=new File("/sdcard"); 

Then it will list all the files only from the sdcard folder .. and if I use

  File f=new File("/download"); 

Then it will list all the files from the download folder .. and if I use

  File f=new File("/"); 

Then it will list only rootcoty root file files ... not files inside / sdcard or / download ..

So, what steps should I follow to list all files with a filter, to list only .csv files from the entire folder inside root.

Thank..

+43
android android-file
Mar 02 2018-12-12T00:
source share
3 answers

Try the following:

  ..... List<File> files = getListFiles(new File("YOUR ROOT")); .... private List<File> getListFiles(File parentDir) { ArrayList<File> inFiles = new ArrayList<File>(); File[] files = parentDir.listFiles(); for (File file : files) { if (file.isDirectory()) { inFiles.addAll(getListFiles(file)); } else { if(file.getName().endsWith(".csv")){ inFiles.add(file); } } } return inFiles; } 

or option without recursion:

 private List<File> getListFiles2(File parentDir) { List<File> inFiles = new ArrayList<>(); Queue<File> files = new LinkedList<>(); files.addAll(Arrays.asList(parentDir.listFiles())); while (!files.isEmpty()) { File file = files.remove(); if (file.isDirectory()) { files.addAll(Arrays.asList(file.listFiles())); } else if (file.getName().endsWith(".csv")) { inFiles.add(file); } } return inFiles; } 
+111
Mar 02 2018-12-12T00:
source share

I changed the decision of Vyacheslav, because I only need the file names inside the directory. Maybe it will be useful for someone.

 ... List<String> files = getList(new File("YOUR ROOT")); ... private List<String> getList(File parentDir, String pathToParentDir) { ArrayList<String> inFiles = new ArrayList<String>(); String[] fileNames = parentDir.list(); for (String fileName : fileNames) { if (fileName.toLowerCase().endsWith(".txt") || fileName.toLowerCase().endsWith(".rtf") || fileName.toLowerCase().endsWith(".txd")) { inFiles.add(pathToParentDir + fileName); } else { File file = new File(parentDir.getPath() + "/" + fileName); if (file.isDirectory()) { inFiles.addAll(getList(file, pathToParentDir + fileName + "/")); } } } return inFiles; } 
+2
Oct 08 '13 at 13:27
source share

You can use the following method,

 private int readLogList( String filePath ) { File directory = Environment.getExternalStorageDirectory(); File folder = new File( directory + ConstantCodes.FILE_SEPARATOR + filePath ); if ( !folder.exists() ) { return 0; } return folder.list().length; } 
+1
02 Mar. 2018-12-12T00:
source share



All Articles