java 创建文件-创建目录-创建临时文件

[java]  view plain  copy
 print ?
  1. import java.io.File;  
  2. import java.io.IOException;  
  3.   
  4. /** 
  5.  * 创建新文件和目录 
  6.  */  
  7. public class CreateFileUtil {  
  8.   
  9.     /** 
  10.      * 创建单个文件 
  11.      * @param destFileName    目标文件名 
  12.      * @return 创建成功,返回true,否则返回false 
  13.      */  
  14.     public static boolean createFile(String destFileName) {  
  15.         File file = new File(destFileName);  
  16.         if (file.exists()) {  
  17.             System.out.println("创建单个文件" + destFileName + "失败,目标文件已存在!");  
  18.             return false;  
  19.         }  
  20.         if (destFileName.endsWith(File.separator)) {  
  21.             System.out.println("创建单个文件" + destFileName + "失败,目标文件不能为目录!");  
  22.             return false;  
  23.         }  
  24.         // 判断目标文件所在的目录是否存在  
  25.         if (!file.getParentFile().exists()) {  
  26.             // 如果目标文件所在的文件夹不存在,则创建父文件夹  
  27.             System.out.println("目标文件所在目录不存在,准备创建它!");  
  28.             if (!file.getParentFile().mkdirs()) {  
  29.                 System.out.println("创建目标文件所在的目录失败!");  
  30.                 return false;  
  31.             }  
  32.         }  
  33.         // 创建目标文件  
  34.         try {  
  35.             if (file.createNewFile()) {  
  36.                 System.out.println("创建单个文件" + destFileName + "成功!");  
  37.                 return true;  
  38.             } else {  
  39.                 System.out.println("创建单个文件" + destFileName + "失败!");  
  40.                 return false;  
  41.             }  
  42.         } catch (IOException e) {  
  43.             e.printStackTrace();  
  44.             System.out  
  45.                     .println("创建单个文件" + destFileName + "失败!" + e.getMessage());  
  46.             return false;  
  47.         }  
  48.     }  
  49.   
  50.     /** 
  51.      * 创建目录 
  52.      * @param destDirName   目标目录名 
  53.      * @return 目录创建成功放回true,否则返回false 
  54.      */  
  55.     public static boolean createDir(String destDirName) {  
  56.         File dir = new File(destDirName);  
  57.         if (dir.exists()) {  
  58.             System.out.println("创建目录" + destDirName + "失败,目标目录已存在!");  
  59.             return false;  
  60.         }  
  61.         if (!destDirName.endsWith(File.separator)) {  
  62.             destDirName = destDirName + File.separator;  
  63.         }  
  64.         // 创建目标目录  
  65.         if (dir.mkdir()) {  
  66.             System.out.println("创建目录" + destDirName + "成功!");  
  67.             return true;  
  68.         } else {  
  69.             System.out.println("创建目录" + destDirName + "失败!");  
  70.             return false;  
  71.         }  
  72.     }  
  73.   
  74.     /** 
  75.      * 创建临时文件 
  76.      * @param prefix    临时文件名的前缀 
  77.      * @param suffix    临时文件名的后缀 
  78.      * @param dirName   临时文件所在的目录,如果输入null,则在用户的文档目录下创建临时文件 
  79.      * @return 临时文件创建成功返回true,否则返回false 
  80.      */  
  81.     public static String createTempFile(String prefix, String suffix,  
  82.             String dirName) {  
  83.         File tempFile = null;  
  84.         if (dirName == null) {  
  85.             try {  
  86.                 // 在默认文件夹下创建临时文件  
  87.                 tempFile = File.createTempFile(prefix, suffix);  
  88.                 // 返回临时文件的路径  
  89.                 return tempFile.getCanonicalPath();  
  90.             } catch (IOException e) {  
  91.                 e.printStackTrace();  
  92.                 System.out.println("创建临时文件失败!" + e.getMessage());  
  93.                 return null;  
  94.             }  
  95.         } else {  
  96.             File dir = new File(dirName);  
  97.             // 如果临时文件所在目录不存在,首先创建  
  98.             if (!dir.exists()) {  
  99.                 if (CreateFileUtil.createDir(dirName)) {  
  100.                     System.out.println("创建临时文件失败,不能创建临时文件所在的目录!");  
  101.                     return null;  
  102.                 }  
  103.             }  
  104.             try {  
  105.                 // 在指定目录下创建临时文件  
  106.                 tempFile = File.createTempFile(prefix, suffix, dir);  
  107.                 return tempFile.getCanonicalPath();  
  108.             } catch (IOException e) {  
  109.                 e.printStackTrace();  
  110.                 System.out.println("创建临时文件失败!" + e.getMessage());  
  111.                 return null;  
  112.             }  
  113.         }  
  114.     }  
  115.   
  116.     public static void main(String[] args) {  
  117.         // 创建目录  
  118.         String dirName = "C:/temp/temp0/temp1";  
  119.         CreateFileUtil.createDir(dirName);  
  120.         // 创建文件  
  121.         String fileName = dirName + "/temp2/tempFile.txt";  
  122.         CreateFileUtil.createFile(fileName);  
  123.         // 创建临时文件  
  124.         String prefix = "temp";  
  125.         String surfix = ".txt";  
  126.         for (int i = 0; i < 10; i++) {  
  127.             System.out.println("创建了临时文件: "  
  128.                     + CreateFileUtil.createTempFile(prefix, surfix, dirName));  
  129.         }  
  130.     }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值