A brief analysis of Java creation files and directories

  • 2020-04-01 03:28:14
  • OfStack

One, the key technical points of creating files and directories are as follows:

    1. CreateNewFile of the File class creates a new empty File according to the abstract path.

        2. The mkdir method of the File class creates a directory according to the abstract path.

        3. The mkdirs method of the File class creates a directory according to the abstract path, including creating a non-existent parent directory & PI;

        4. CreateTempFile method of the File class creates a temporary File. You can specify the prefix, suffix and directory of the File name of the temporary File, if you do not specify & PI;                         The directory is stored in a temporary folder on the system.  

          5. In addition to mkdirs method, the above method must ensure that the target file does not exist and the parent directory does when creating files and directories, otherwise the creation will fail  

Ii. The example 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, target file cannot be directory! "); 
      return false; 
    } 
    //Determines if the directory where the target file resides exists
    if(!file.getParentFile().exists()) { 
      //If the directory where the target file resides does not exist, the parent directory is created
      System.out.println(" The directory of the target file does not exist, ready to create it! "); 
      if(!file.getParentFile().mkdirs()) { 
        System.out.println(" Failed to create target file directory! "); 
        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 + " Failure! " + e.getMessage()); 
      return false; 
    } 
  }  
  public static boolean createDir(String destDirName) { 
    File dir = new File(destDirName); 
    if (dir.exists()) { 
      System.out.println(" Create a directory " + destDirName + " Failed, the target directory already exists "); 
      return false; 
    } 
    if (!destDirName.endsWith(File.separator)) { 
      destDirName = destDirName + File.separator; 
    } 
    //Create a directory
    if (dir.mkdirs()) { 
      System.out.println(" Create a directory " + destDirName + " Success! "); 
      return true; 
    } else { 
      System.out.println(" Create a directory " + destDirName + " Failure! "); 
      return false; 
    } 
  }    
  public static String createTempFile(String prefix, String suffix, String dirName) { 
    File tempFile = null; 
    if (dirName == null) { 
      try{ 
        //Create temporary files under the default folder
        tempFile = File.createTempFile(prefix, suffix); 
        //Returns the path to the temporary file
        return tempFile.getCanonicalPath(); 
      } catch (IOException e) { 
        e.printStackTrace(); 
        System.out.println(" Temporary file creation failed! " + e.getMessage()); 
        return null; 
      } 
    } else { 
      File dir = new File(dirName); 
      //If the directory where the temporary file resides does not exist, create it first
      if (!dir.exists()) { 
        if (!CreateFileUtil.createDir(dirName)) { 
          System.out.println(" Temporary file creation failed, cannot create temporary file directory! "); 
          return null; 
        } 
      } 
      try { 
        //Creates a temporary file in the specified directory
        tempFile = File.createTempFile(prefix, suffix, dir); 
        return tempFile.getCanonicalPath(); 
      } catch (IOException e) { 
        e.printStackTrace(); 
        System.out.println(" Temporary file creation failed! " + e.getMessage()); 
        return null; 
      } 
    } 
  } 
   
  public static void main(String[] args) { 
    //Create a directory
    String dirName = "D:/work/temp/temp0/temp1"; 
    CreateFileUtil.createDir(dirName); 
    //Create a file
    String fileName = dirName + "/temp2/tempFile.txt"; 
    CreateFileUtil.createFile(fileName); 
    //Create temporary file
    String prefix = "temp"; 
    String suffix = ".txt"; 
    for (int i = 0; i < 10; i++) { 
      System.out.println(" Created temporary file: " 
          + CreateFileUtil.createTempFile(prefix, suffix, dirName)); 
    } 
    // In the default directory Create temporary file
    for (int i = 0; i < 10; i++) { 
      System.out.println(" Temporary files are created in the default directory: " 
          + CreateFileUtil.createTempFile(prefix, suffix, null)); 
    } 
  } 
 
} 

Output:      


 Create a directory D:/work/temp/temp0/temp1 Success!  
 The directory of the target file does not exist, ready to create it!  
 Create a single file D:/work/temp/temp0/temp1/temp2/tempFile.txt Success!  
 Created temporary file: D:work emp emp0 emp1 emp5171.txt 
 Created temporary file: D:work emp emp0 emp1 emp5172.txt 
 Created temporary file: D:work emp emp0 emp1 emp5173.txt 
 Created temporary file: D:work emp emp0 emp1 emp5174.txt 
 Created temporary file: D:work emp emp0 emp1 emp5175.txt 
 Created temporary file: D:work emp emp0 emp1 emp5176.txt 
 Created temporary file: D:work emp emp0 emp1 emp5177.txt 
 Created temporary file: D:work emp emp0 emp1 emp5178.txt 
 Created temporary file: D:work emp emp0 emp1 emp5179.txt 
 Created temporary file: D:work emp emp0 emp1 emp5180.txt 
 Temporary files are created in the default directory: C:Documents and SettingsAdministratorLocal SettingsTemp emp5181.txt 
 Temporary files are created in the default directory: C:Documents and SettingsAdministratorLocal SettingsTemp emp5182.txt 
 Temporary files are created in the default directory: C:Documents and SettingsAdministratorLocal SettingsTemp emp5183.txt 
 Temporary files are created in the default directory: C:Documents and SettingsAdministratorLocal SettingsTemp emp5184.txt 
 Temporary files are created in the default directory: C:Documents and SettingsAdministratorLocal SettingsTemp emp5185.txt 
 Temporary files are created in the default directory: C:Documents and SettingsAdministratorLocal SettingsTemp emp5186.txt 
 Temporary files are created in the default directory: C:Documents and SettingsAdministratorLocal SettingsTemp emp5187.txt 
 Temporary files are created in the default directory: C:Documents and SettingsAdministratorLocal SettingsTemp emp5188.txt 
 Temporary files are created in the default directory: C:Documents and SettingsAdministratorLocal SettingsTemp emp5189.txt 
 Temporary files are created in the default directory: C:Documents and SettingsAdministratorLocal SettingsTemp emp5190.txt 


Related articles: