文件操作

package com.chinabase.common.util;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * 文件夹
 * */
public class Folder {
    /**
     * 创建文件夹
     * 
     * @param path
     * @return
     */
    public static boolean createFolder(String path) {
        boolean result = false;
        File dirFile = null;
        try {
            dirFile = new File(path);
            if (!(dirFile.exists()) && !(dirFile.isDirectory())) {
                boolean creadok = dirFile.mkdirs();
                if (creadok) {
                    // System.out.println(" ok:创建文件夹成功! ");
                    result = true;
                } else {
                    // System.out.println(" err:创建文件夹失败! ");
                    result = false;
                }
            } else {
                result = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = false;
        }

        return result;
    }

    /**
     * 删除文件夹
     * 
     * @param folder
     * @return
     */
    public static boolean deleteFolder(File folder) {
        boolean result = false;
        if (!(folder.exists())) {
            result = true;
            return result;
        }
        try {
            String childs[] = folder.list();
            if (childs == null || childs.length <= 0) {
                if (folder.delete()) {
                    result = true;
                }
            } else {
                for (int i = 0; i < childs.length; i++) {
                    String childName = childs[i];
                    String childPath = folder.getPath() + File.separator
                            + childName;
                    File filePath = new File(childPath);
                    if (filePath.exists() && filePath.isFile()) {
                        if (filePath.delete()) {
                            result = true;
                        } else {
                            result = false;
                            break;
                        }
                    } else if (filePath.exists() && filePath.isDirectory()) {
                        if (deleteFolder(filePath)) {
                            result = true;
                        } else {
                            result = false;
                            break;
                        }
                    }
                }
            }

            folder.delete();
        } catch (Exception e) {
            e.printStackTrace();
            result = false;
        }
        return result;
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // createFolder("c://apache-tomcat-6.0.10//work//Catalina//localhost");
         File folder=new File("G:/news");
         String[] s=folder.list();
         for (int i = 0; i < s.length; i++) {
            String string = s[i];
            System.out.println(string );
        }
        // deleteFolder(folder);
//        File file=new File("F:/workspace/xiwa/WebRoot/upload/admin_attachTemp/index_list.jsp");
//        System.out.println(file.exists());
//        long begin = System.nanoTime();
//        int[] a = { 0, 1, 2, -1, 3, -1, -3, 3, -4, 5, 4 }; // 0 2 5
//        Map temp = new HashMap();
//        Map record = new HashMap();
//        for (int i = 0; i < a.length; i++) {
//            temp.put(i, a[i]);
//        }
//        boolean flag = false;
//        for (int i = 0; i < a.length; i++) {
//            if (record.get(i) == null) {
//                int m = Math.abs(a[i]);
//                flag = false;
//                for (int j = i + 1; j < a.length; j++) {
//                    if (record.get(j) == null) {
//                        int n = Math.abs(a[j]);
//                        if (m == n) {
//                            temp.remove(j);
//                            record.put(j, j);
//                            flag = true;
//                        }
//                    }
//                }
//                if (flag && temp.get(i) != null) {
//                    temp.remove(i);
//                    record.put(i, i);
//                }
//            }
//        }
//    
//        Iterator it = temp.entrySet().iterator();
//        while (it.hasNext()) {
//            Map.Entry entry = (Map.Entry) it.next();
//            System.out.println(entry.getValue());
//        }
//        System.out.println("用时:" + (System.nanoTime() - begin)+"毫微秒");
        
    }
}
原文地址:https://www.cnblogs.com/kedoudejingshen/p/3511785.html