Java new File新建文件夹与文件,并删除文件demo

  /**
     * 创建文件夹
     * */
    public static boolean mkDirectory(String path) {
        File file = null;
        try {
            file = new File(path);
            if (!file.exists()) {
                return file.mkdirs();
            }
            else{
                return false;
            }
        } catch (Exception e) {
        } finally {
            file = null;
        }
        return false;
    }

/** * 新增文件夹与文件后进行删除 * */ public void test(){ String path = "/test/abc/"; String fileName = "file1.txt"; if(mkDirectory(path)){ log.info("mkdir ok "); } try{ File dir = new File(path); File file1 = new File(dir, fileName); file1.createNewFile(); }catch (IOException e){ log.info("error:" + e.getMessage()); } File file = new File(path + fileName); if (file != null) { if(file.exists()){ file.delete(); log.info("file:" + file.getAbsolutePath() + " del success"); file = null; }else{ log.info("file:" + file.getAbsolutePath() + " no exists"); } file = null; } }
原文地址:https://www.cnblogs.com/todarcy/p/15048235.html