读取文件夹下所有文件,包含子目录

    1. /**
    2. * 读取某个文件夹下的所有文件
    3. */
    4. public static boolean readfile(String filepath) throws FileNotFoundException, IOException {
    5. try {
    6. File file = new File(filepath);
    7. if (!file.isDirectory()) {
    8. System.out.println("文件");
    9. System.out.println("path=" + file.getPath());
    10. System.out.println("absolutepath=" + file.getAbsolutePath());
    11. System.out.println("name=" + file.getName());
    12. } else if (file.isDirectory()) {
    13. System.out.println("文件夹");
    14. String[] filelist = file.list();
    15. for (int i = 0; i < filelist.length; i++) {
    16. File readfile = new File(filepath + "\\" + filelist[i]);
    17. if (!readfile.isDirectory()) {
    18. String searchName = readfile.getName().substring(0,readfile.getName().lastIndexOf("."));
    19. System.out.println("{ \"searchName\": \"" + searchName + "\", \"imgName\": \"" + readfile.getName() + "\" },");
    20. } else if (readfile.isDirectory()) {
    21. readfile(filepath + "\\" + filelist[i]);
    22. }
    23. }
    24. }
    25. } catch (FileNotFoundException e) {
    26. System.out.println("readfile() Exception:" + e.getMessage());
    27. }
    28. return true;
    29. }