zoukankan      html  css  js  c++  java
  • 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; } }
  • 相关阅读:
    UVA11039
    UVA10970大块巧克力
    UVA10970大块巧克力
    UVA10340子序列
    UVA10340子序列
    UVA10382喷水装置
    UVA10382喷水装置
    UVA10020(最小区间覆盖)
    洛谷 P2141 珠心算测验
    UVa 11292 勇者斗恶龙(The Dragon of Loowater)
  • 原文地址:https://www.cnblogs.com/todarcy/p/15048235.html
Copyright © 2011-2022 走看看