[Java] java创建文件 →→→→→进入此内容的聊天室

来自 , 2019-08-28, 写在 Java, 查看 120 次.
URL http://www.code666.cn/view/851ddf50
  1.         /**
  2.          * 创建单个文件
  3.          *
  4.          * @param destFileName
  5.          *            目标文件名
  6.          * @return 创建成功,返回true,否则返回false
  7.          */
  8.         public static boolean createFile(String destFileName) {
  9.                 File file = new File(destFileName);
  10.                 if (file.exists()) {
  11.                         System.out.println("创建单个文件" + destFileName + "失败,目标文件已存在!");
  12.                         return false;
  13.                 }
  14.                 if (destFileName.endsWith(File.separator)) {
  15.                         System.out.println("创建单个文件" + destFileName + "失败,目标文件不能为目录!");
  16.                         return false;
  17.                 }
  18.                 // 判断目标文件所在的目录是否存在
  19.                 if (!file.getParentFile().exists()) {
  20.                         // 如果目标文件所在的文件夹不存在,则创建父文件夹
  21.                         System.out.println("目标文件所在目录不存在,准备创建它!");
  22.                         if (!file.getParentFile().mkdirs()) {
  23.                                 System.out.println("创建目标文件所在的目录失败!");
  24.                                 return false;
  25.                         }
  26.                 }
  27.                 // 创建目标文件
  28.                 try {
  29.                         if (file.createNewFile()) {
  30.                                 System.out.println("创建单个文件" + destFileName + "成功!");
  31.                                 return true;
  32.                         } else {
  33.                                 System.out.println("创建单个文件" + destFileName + "失败!");
  34.                                 return false;
  35.                         }
  36.                 } catch (IOException e) {
  37.                         e.printStackTrace();
  38.                         System.out
  39.                                         .println("创建单个文件" + destFileName + "失败!" + e.getMessage());
  40.                         return false;
  41.                 }
  42.         }
  43.  

回复 "java创建文件"

这儿你可以回复上面这条便签

captcha