1. The key technical points for creating files and directories are as follows:
1. CreateNewFile of the File class creates a new empty file based on the abstract path. When the file specified by the abstract path exists, the creation fails.
2. The mkdir method of the File class creates a directory based on the abstract path.
3. The mkdirs method of the File class creates directories based on abstract paths, including creating non-existent parent directories.
4. The createTempFile method of the File class creates a temporary file. You can specify the file name prefix, suffix and directory where the file is located. If the directory is not specified, it will be stored in the system's temporary folder.
5. Except for the mkdirs method, when creating files and directories with the above methods, you must ensure that the target file does not exist and the parent directory exists, otherwise the creation will fail.
Second, the example demonstration is as follows:
import java.io.File; import java.io.IOException; public class CreateFileUtil { public static boolean createFile(String destFileName) { File file = new File(destFileName); if(file.exists()) { System.out.println ("Create a single file" + destFileName + "Failed, the target file already exists!"); return false; } if (destFileName.endsWith(File.separator)) { System.out.println("Create a single file" + destFileName + "Failed, the target file cannot be a directory!"); return false; } //Determine whether the directory where the target file is located existsif(!file.getParentFile().exists()) { //If the directory where the target file is located does not exist, create the parent directory System.out.println("The directory where the target file is located does not exist, prepare to create it!"); if(!file.getParentFile().mkdirs()) { System.out.println("Failed to create the directory where the target file is located!"); return false; } } //Create the target file try { if (file.createNewFile()) { System.out.println("Create a single file" + destFileName + "Success!"); return true; } else { System.out.println("Create a single file" + destFileName + "Failure!"); return false; } } catch (IOException e) { e.printStackTrace() ; System.out.println("Create a single file" + destFileName + "Failed!" + e.getMessage()); return false; } } public static boolean createDir(String destDirName) { File dir = new File(destDirName); if (dir.exists()) { System.out.println("Create directory" + destDirName + "Failed, the target directory already exists"); return false; } if (!destDirName.endsWith(File.separator)) { destDirName = destDirName + File.separator; } //Create directory if (dir.mkdirs()) { System.out.println("Create directory" + destDirName + "Success! "); return true; } else { System.out.println("Create directory" + destDirName + "Failed!"); return false; } } public static String createTempFile(String prefix, String suffix, String dirName) { File tempFile = null; if (dirName == null) { try{ //Create a temporary file in the default folder tempFile = File.createTempFile(prefix, suffix); //Return the path of the temporary file return tempFile.getCanonicalPath(); } catch (IOException e) { e.printStackTrace(); System.out.println("Failed to create temporary file!" + e.getMessage()) ; return null; } } else { File dir = new File(dirName); //If the directory where the temporary file is located does not exist, create it first if (!dir.exists()) { if (!CreateFileUtil.createDir(dirName)) { System.out.println("Failed to create a temporary file, the directory where the temporary file is located cannot be created!"); return null; } } try { //Create a temporary file tempFile in the specified directory = File.createTempFile(prefix, suffix, dir); return tempFile.getCanonicalPath(); } catch (IOException e) { e.printStackTrace(); System.out.println("Failed to create temporary file!" + e.getMessage()); return null; } } } public static void main(String[] args) { //Create directory String dirName = "D:/work/temp/temp0/temp1"; CreateFileUtil.createDir(dirName); //Create file String fileName = dirName + "/temp2/tempFile.txt"; CreateFileUtil.createFile(fileName); //Create a temporary file String prefix = "temp"; String suffix = ".txt"; for (int i = 0; i < 10; i++) { System.out.println("Created a temporary file: " + CreateFileUtil.createTempFile(prefix, suffix, dirName)); } //Create a temporary file in the default directory for (int i = 0; i < 10; i++) { System.out.println("A temporary file was created in the default directory:" + CreateFileUtil.createTempFile(prefix, suffix, null )); } } }Output result:
Directory D:/work/temp/temp0/temp1 was created successfully! The directory where the target file is located does not exist, prepare to create it! Created a single file D:/work/temp/temp0/temp1/temp2/tempFile.txt successfully! A temporary file was created: D:work emp emp0 emp1 emp5171.txt A temporary file was created: D:work emp emp0 emp1 emp5172.txt A temporary file was created: D:work emp emp0 emp1 emp5173.txt A temporary file was created: D:work emp emp0 emp1 emp5174.txt Temporary file created: D:work emp emp0 emp1 emp5175.txt Temporary file created: D:work emp emp0 emp1 emp5176.txt Temporary file created: D:work emp emp0 emp1 emp5177.txt Temporary file created: D:work emp emp0 emp1 emp5178.txt Created Temporary files: D:work emp emp0 emp1 emp5179.txt Temporary files were created: D:work emp emp0 emp1 emp5180.txt Temporary files were created in the default directory: C:Documents and SettingsAdministratorLocal SettingsTemp emp5181.txt Temporary files were created in the default directory: C:Documents and SettingsAdministratorLocal SettingsTemp emp5182.txt A temporary file was created in the default directory: C:Documents and SettingsAdministratorLocal SettingsTemp emp5183.txt A temporary file was created in the default directory: C:Documents and SettingsAdministratorLocal SettingsTemp emp5184.txt A temporary file was created in the default directory: C:Documents and SettingsAdministratorLocal SettingsTemp emp5185.txt A temporary file was created in the default directory: C:Documents and SettingsAdministratorLocal SettingsTemp emp5186.txt A temporary file was created in the default directory: C:Documents and SettingsAdministratorLocal SettingsTemp emp5187.txt A temporary file was created in the default directory: C:Documents and SettingsAdministratorLocal SettingsTemp emp5188.txt A temporary file was created in the default directory: C:Documents and SettingsAdministratorLocal SettingsTemp emp5189.txt A temporary file was created in the default directory: C:Documents and SettingsAdministratorLocal SettingsTemp emp5190.txt