When developing with java, it reads files in folders and read folders in folders, so it is easy to read at once, so it’s an easy source, but share it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
import java.io.File; import java.util.ArrayList; public class FolderManager { private ArrayList<File> fileList = new ArrayList<File>(); private File files[] = null; /** * * @author diffmension * @param path *@return File[] */ public File[] reading(String path){ File data[] = null; File folder = null; try { folder = new File(path); data = folder.listFiles(); for (int m = 0; m < data.length; m++) { boolean ret = data[m].isDirectory(); if (ret) { reading(data[m].getPath()); } else { fileList.add(data[m]); } } for (int n = 0; n < fileList.size(); n++) { // System.out.println(NewAll.get(n)); } files = new File[fileList.size()]; files = (File[]) fileList.toArray(new File[0]); if (data.length > 0) { System.out.println(path + "Read Success"); } else { System.out.println(path + "No File Inside"); } } catch (Exception e) { System.out.println("Read Failure.Verify The Path" + e.toString()); } return files; } //test main public static void main(String[] args) { FolderManager formerReading =new FolderManager(); File [] file=null; file=formerReading.reading("./log"); int x=file.length; System.out.println("---------------------"); for (int m = 0; m <x; m++) { System.out.println(file[m]); } } } |