createNewFile创建空文件夹与createTempFile创建临时文件夹

创建文件和目录的关键技术点如下:      

  • <pre name="code" class="java">   
  • File类的createNewFile根据抽象路径创建一个新的空文件,当抽象路径制定的文件存在时,创建失败  
  • File类的mkdir方法根据抽象路径创建目录  
  • File类的mkdirs方法根据抽象路径创建目录,包括创建必需但不存在的父目录  
  • File类的createTempFile方法创建临时文件,可以制定临时文件的文件名前缀、后缀及文件所在的目录,如果不指定目录,则存放在系统的临时文件夹下。  
  • 除mkdirs方法外,以上方法在创建文件和目录时,必须保证目标文件不存在,而且父目录存在,否则会创建失败  
  实例演示:  
  1 package book.io;

  2 

  3 import java.io.File;

  4 import java.io.IOException;

  5 

  6 public class CreateFileUtil {

  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.println("创建单个文件" + destFileName + "失败!" + e.getMessage());

 39             return false;

 40         }

 41     }

 42    

 43    

 44     public static boolean createDir(String destDirName) {

 45         File dir = new File(destDirName);

 46         if (dir.exists()) {

 47             System.out.println("创建目录" + destDirName + "失败,目标目录已经存在");

 48             return false;

 49         }

 50         if (!destDirName.endsWith(File.separator)) {

 51             destDirName = destDirName + File.separator;

 52         }

 53         //创建目录

 54         if (dir.mkdirs()) {

 55             System.out.println("创建目录" + destDirName + "成功!");

 56             return true;

 57         } else {

 58             System.out.println("创建目录" + destDirName + "失败!");

 59             return false;

 60         }

 61     }

 62    

 63    

 64     public static String createTempFile(String prefix, String suffix, String dirName) {

 65         File tempFile = null;

 66         if (dirName == null) {

 67             try{

 68                 //在默认文件夹下创建临时文件

 69                 tempFile = File.createTempFile(prefix, suffix);

 70                 //返回临时文件的路径

 71                 return tempFile.getCanonicalPath();

 72             } catch (IOException e) {

 73                 e.printStackTrace();

 74                 System.out.println("创建临时文件失败!" + e.getMessage());

 75                 return null;

 76             }

 77         } else {

 78             File dir = new File(dirName);

 79             //如果临时文件所在目录不存在,首先创建

 80             if (!dir.exists()) {

 81                 if (!CreateFileUtil.createDir(dirName)) {

 82                     System.out.println("创建临时文件失败,不能创建临时文件所在的目录!");

 83                     return null;

 84                 }

 85             }

 86             try {

 87                 //在指定目录下创建临时文件

 88                 tempFile = File.createTempFile(prefix, suffix, dir);

 89                 return tempFile.getCanonicalPath();

 90             } catch (IOException e) {

 91                 e.printStackTrace();

 92                 System.out.println("创建临时文件失败!" + e.getMessage());

 93                 return null;

 94             }

 95         }

 96     }

 97    

 98     public static void main(String[] args) {

 99         //创建目录

100         String dirName = "D:/work/temp/temp0/temp1";

101         CreateFileUtil.createDir(dirName);

102         //创建文件

103         String fileName = dirName + "/temp2/tempFile.txt";

104         CreateFileUtil.createFile(fileName);

105         //创建临时文件

106         String prefix = "temp";

107         String suffix = ".txt";

108         for (int i = 0; i < 10; i++) {

109             System.out.println("创建了临时文件:"

110                     + CreateFileUtil.createTempFile(prefix, suffix, dirName));

111         }

112         //在默认目录下创建临时文件

113         for (int i = 0; i < 10; i++) {

114             System.out.println("在默认目录下创建了临时文件:"

115                     + CreateFileUtil.createTempFile(prefix, suffix, null));

116         }

117     }

118 

119 }
输出结果: 

123 创建目录D:/work/temp/temp0/temp1成功!

124 目标文件所在目录不存在,准备创建它!

125 创建单个文件D:/work/temp/temp0/temp1/temp2/tempFile.txt成功!

126 创建了临时文件:D:work emp emp0 emp1 emp5171.txt

127 创建了临时文件:D:work emp emp0 emp1 emp5172.txt

128 创建了临时文件:D:work emp emp0 emp1 emp5173.txt

129 创建了临时文件:D:work emp emp0 emp1 emp5174.txt

130 创建了临时文件:D:work emp emp0 emp1 emp5175.txt

131 创建了临时文件:D:work emp emp0 emp1 emp5176.txt

132 创建了临时文件:D:work emp emp0 emp1 emp5177.txt

133 创建了临时文件:D:work emp emp0 emp1 emp5178.txt

134 创建了临时文件:D:work emp emp0 emp1 emp5179.txt

135 创建了临时文件:D:work emp emp0 emp1 emp5180.txt

136 在默认目录下创建了临时文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5181.txt

137 在默认目录下创建了临时文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5182.txt

138 在默认目录下创建了临时文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5183.txt

139 在默认目录下创建了临时文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5184.txt

140 在默认目录下创建了临时文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5185.txt

141 在默认目录下创建了临时文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5186.txt

142 在默认目录下创建了临时文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5187.txt

143 在默认目录下创建了临时文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5188.txt

144 在默认目录下创建了临时文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5189.txt

145 在默认目录下创建了临时文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5190.txt

 

 

你可能感兴趣的:(create)