码迷,mamicode.com
首页 > 编程语言 > 详细

递归读取制定目录下所有文件夹和文件的实现(java)

时间:2018-05-13 16:53:20      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:pat   dir   getname   catch   too   except   cat   ons   name   

  public static String getAllDirectorisAndFiles(String path){
    	Map<String, Object> responseMap = new HashMap<String, Object>();
		responseMap.put("time_stamp", Tools.currentTime());
		String responseString = "";
		try {
			getDirectorisAndFiles(path, responseMap, "data");
			responseString = (new Gson()).toJson(responseMap);
		} catch (Exception e) {
			System.out.println("files列表读取异常");
		}
		return responseString;
    }
    
    public static void getDirectorisAndFiles(String path, Map<String, Object> resultMap, String directoryName) {
    	System.out.println(path);
    	System.out.println(directoryName);
    	File file = new File(path);
    	ArrayList<Object> dataArray = new ArrayList<Object>();
        File [] files = file.listFiles();
        
        for(File a:files)
        {
            if(a.isDirectory())
            {
            	Map<String, Object> tempMap = new HashMap<String, Object>();
            	getDirectorisAndFiles(path + a.getName() + "/", tempMap, a.getName());
            	dataArray.add(tempMap);
            }else{
            	dataArray.add(a.getName());
            }
        }
        resultMap.put(directoryName, dataArray);
	}

  

递归读取制定目录下所有文件夹和文件的实现(java)

标签:pat   dir   getname   catch   too   except   cat   ons   name   

原文地址:https://www.cnblogs.com/yangzigege/p/9032519.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!