Brief analysis of Java creation file and directory _java

Source: Internet
Author: User
Tags create directory getmessage parent directory

First, the key technical points for creating files and directories are as follows:

1, the CreateNewFile of the file class creates a new empty file based on the abstract path, and when the abstract path is made to the file, the creation fails

2, the MkDir method of the file class to create a directory based on the abstract path

3. The Mkdirs method of the file class creates a directory based on the abstract path, including creating a non-existent parent directory

4, the Createtempfile method of the file class to create temporary files, you can make temporary file file name prefix, suffix and file directory, if not specified directory, then stored in the system's temporary folder.

5, in addition to Mkdirs methods, the above methods in the creation of files and directories, you must ensure that the target file does not exist, and the parent directory exists, or you will create a failure

Second, the example demo is as follows:

Import Java.io.File; 
Import java.io.IOException; public class Createfileutil {public static Boolean CreateFile (String destfilename) {File File = new file (dest 
    FileName); if (file.exists ()) {System.out.println ("Create a single file + destFileName +" failed, the destination file already exists!) 
      "); 
    return false; } if (Destfilename.endswith (File.separator)) {System.out.println ("Create a single file + destFileName +" failed, the destination file cannot be a directory!) 
      "); 
    return false; //Determine if the directory in which the destination file resides exists if (!file.getparentfile (). exists ()) {//If the directory in which the destination file is located does not exist, create the parent directory System.out.printl N ("The destination file directory does not exist, prepare to create it!") 
      "); if (!file.getparentfile (). Mkdirs ()) {System.out.println () failed to create destination file directory! 
        "); 
      return false;  }///create 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 +) failed! 
        "); 
      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, 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 temporary file under 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 ("Create temp file failed!") 
        "+ e.getmessage ()); 
      return null; 
      } else {File dir = new file (dirname); If the directory in which the temporary file resides does not exist, first create the IF (!dir.exists ()) {if (!) Createfileutil.createdir (dirname)) {System.out.println ("Create temporary file failed, cannot create temporary file directory!") 
          "); 
        return null; 
        try {//create temporary file under specified directory tempfile = file.createtempfile (prefix, suffix, dir); 
      return Tempfile.getcanonicalpath (); 
        catch (IOException e) {e.printstacktrace (); SYSTEM.OUT.PRINTLN ("Create temp file failed!") 
        "+ 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 temporary file StRing prefix = "temp"; 
    String suffix = ". txt"; for (int i = 0; i < i++) {System.out.println ("created temporary file:" + createfileutil.createtempfile (prefix, 
    suffix, dirname)); Create a temporary file in the default directory for (int i = 0; i < i++) {System.out.println ("Create temporary file under Default directory:" + creat 
    Efileutil.createtempfile (prefix, suffix, null)); 

 } 
  } 
 
}

Output result:    

Create directory D:/WORK/TEMP/TEMP0/TEMP1 Successful! 
Destination file directory does not exist, ready to create it! 
Create a single file D:/work/temp/temp0/temp1/temp2/tempfile.txt success! Temporary file created: d:work emp emp0 emp1 emp5171.txt created temporary files: d:work emp emp0 emp1 The Emp5172.txt created temporary files: d:work emp emp0 EMP1 XT created temporary files: d:work emp emp0 emp1 emp5174.txt created temporary files: d:work emp emp0 emp1 The Emp5175.txt created temporary files: d:work emp emp0 EMP1 76.txt created temporary file: d:work emp emp0 emp1 emp5177.txt created temporary files: d:work emp emp0 emp1 The Emp5178.txt created temporary files: d:work emp emp0 emp1 E Mp5179.txt created temporary files: d:work emp emp0 emp1 emp5180.txt created temporary files under the default directory: C:documents and settingsadministratorlocal Settingste MP Emp5181.txt created temporary files under the default directory: C:documents and settingsadministratorlocal settingstemp Emp5182.txt created temporary files under the default directory: C:d Ocuments and settingsadministratorlocal settingstemp Emp5183.txt created temporary files under the default directory: C:documents and Settingsadministratorlocal Settingstemp Emp5184.txt created a temporary file under the default directory: C:documents and Settingsadministratorlocal Settingstemp Emp5185.txt created a temporary file under the default directory: C:documents and SettingsadministratorlocalSettingstemp Emp5186.txt created a temporary file under the default directory: C:documents and settingsadministratorlocal settingstemp emp5187.txt Temporary files were created under the default directory: C:documents and settingsadministratorlocal settingstemp Emp5188.txt created temporary files under the default directory: C:documents and Settingsadministratorlocal Settingstemp Emp5189.txt created a temporary file under the default directory: C:documents and Settingsadministratorlocal 
 Settingstemp Emp5190.txt

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.