Java create temporary file

From , 5 Years ago, written in Java, viewed 168 times.
URL https://pastebin.vip/view/7750ca35
  1.         /**
  2.          * 创建临时文件
  3.          *
  4.          * @param prefix
  5.          *            临时文件名的前缀
  6.          * @param suffix
  7.          *            临时文件名的后缀
  8.          * @param dirName
  9.          *            临时文件所在的目录,如果输入null,则在用户的文档目录下创建临时文件
  10.          * @return 临时文件创建成功返回true,否则返回false
  11.          */
  12.         public static String createTempFile(String prefix, String suffix,
  13.                         String dirName) {
  14.                 File tempFile = null;
  15.                 if (dirName == null) {
  16.                         try {
  17.                                 // 在默认文件夹下创建临时文件
  18.                                 tempFile = File.createTempFile(prefix, suffix);
  19.                                 // 返回临时文件的路径
  20.                                 return tempFile.getCanonicalPath();
  21.                         } catch (IOException e) {
  22.                                 e.printStackTrace();
  23.                                 System.out.println("创建临时文件失败!" + e.getMessage());
  24.                                 return null;
  25.                         }
  26.                 } else {
  27.                         File dir = new File(dirName);
  28.                         // 如果临时文件所在目录不存在,首先创建
  29.                         if (!dir.exists()) {
  30.                                 if (CreateFileUtil.createDir(dirName)) {
  31.                                         System.out.println("创建临时文件失败,不能创建临时文件所在的目录!");
  32.                                         return null;
  33.                                 }
  34.                         }
  35.                         try {
  36.                                 // 在指定目录下创建临时文件
  37.                                 tempFile = File.createTempFile(prefix, suffix, dir);
  38.                                 return tempFile.getCanonicalPath();
  39.                         } catch (IOException e) {
  40.                                 e.printStackTrace();
  41.                                 System.out.println("创建临时文件失败!" + e.getMessage());
  42.                                 return null;
  43.                         }
  44.                 }
  45.         }
  46.  

Reply to "Java create temporary file"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website