Java Creating files and directories

Source: Internet
Author: User

Tag:cal   static   filename    system    etc    work   for   get   set   

The key technical points for creating files and directories are as follows: [Java] View plain copy<pre name= "code" class= "Java" > 1, File Class CreateNewFile create a new empty file based on an abstract path, when the abstract road The mkdir method of the file class creates the directory 3 according to the abstract path, the Mkdirs method of the file class creates the directory according to the abstract path, including the Createtempfile method of creating the required but nonexistent parent directory 4, the file class.      Create temporary files, you can set the temporary file filename prefix, suffix and the directory where the file is located, if you do not specify a directory, it is stored in the system's temporary folder.    5, in addition to the Mkdirs method, the above method in the creation of files and directories, must ensure that the target file does not exist, and the parent directory exists, otherwise it will create a failed instance demo [Java] View plain copypackage Book.io;  Import Java.io.File;    Import java.io.IOException; public class Createfileutil {public static Boolean createFile (String destfilename) {File File = new F          Ile (destFileName); if (file.exists ()) {System.out.println ("Create single file" + destFileName + "failed, destination file already exists!              ");          return false; } if (Destfilename.endswith (File.separator)) {System.out.println ("Create single file" + destFileName + "failed, destination file cannot be a directory!              ");          return false;            }//Determine if the directory where the target file resides exists if (!file.getparentfile (). exists ()) {  If the destination file is in a directory that does not exist, create the parent directory System.out.println ("The destination file is in a directory that does not exist, and you are ready to create it!")              "); if (!file.getparentfile (). Mkdirs ()) {System.out.println ("the directory where the target file was created failed!                  ");              return false; }}//create target file try {if (File.createnewfile ()) {System.out.println ("Create Single file" + destFileName + "Success!")                  ");              return true; } else {System.out.println ("Create single file" + destFileName + "failed!                  ");              return false;              }} catch (IOException e) {e.printstacktrace (); System.out.println ("Create 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 ("Failed to create temp file!                  "+ e.getmessage ());              return null;        }} else {file dir = new file (dirName);      If the temporary file is located in a directory that does not exist, first create if (!dir.exists ()) {if (! Createfileutil.createdir (DirName)) {System.out.println ("failed to create temporary file, cannot create the directory where temporary files are located!                      ");                  return null; }} try {//create temporary files under the specified directory tempfile = file.createtempfile (prefix                  , suffix, dir);              return Tempfile.getcanonicalpath ();                  } catch (IOException e) {e.printstacktrace (); System.out.println ("Failed to create temp 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 temporary file String prefix = "temp"; String suffix =". txt"; for (int i = 0; i < i++) {System.out.println ("created temporary file:" + createfileutil.createt          Empfile (prefix, suffix, dirName));                      }//create temporary file in default directory for (int i = 0; i < i++) {System.out.println ("temporary file created in default directory:")          + createfileutil.createtempfile (prefix, suffix, null));  }}} Output: Create directory D:/WORK/TEMP/TEMP0/TEMP1 success!  The directory where the target file is located does not exist, ready to create it!  Create a single file D:/work/temp/temp0/temp1/temp2/tempfile.txt success! Temporary files created: d:work emp emp0 emp1 emp5171.txt created temporary files: d:work emp emp0 emp1 emp5172.txt created temporary files: d:work emp emp0 emp1 emp5173.t XT created temporary files: d:work emp emp0 emp1 emp5174.txt created temporary files: d:work emp emp0 emp1 emp5175.txt created temporary files: d:work emp emp0 emp1 emp51 76.txt created temporary files: d:work emp emp0 emp1 emp5177.txt created temporary files: d:work emp emp0 emp1 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 in default directory: C:documents and settingsadministratorlocal SetTingstemp Emp5181.txt created temporary files in default directory: C:documents and settingsadministratorlocal settingstemp emp5182.txt Temporary files were created in the default directory: C:documents and settingsadministratorlocal settingstemp Emp5183.txt created temporary files in the default directory: C:documents and Settingsadministratorlocal Settingstemp Emp5184.txt created a temporary file in the default directory: C:documents and Settingsadministratorlocal  Settingstemp Emp5185.txt created temporary files in default directory: C:documents and settingsadministratorlocal settingstemp emp5186.txt Temporary files were created in the default directory: C:documents and settingsadministratorlocal settingstemp Emp5187.txt created temporary files in the default directory: C:documents and Settingsadministratorlocal Settingstemp Emp5188.txt created a temporary file in the default directory: C:documents and Settingsadministratorlocal   Settingstemp Emp5189.txt created temporary files in default directory: C:documents and settingsadministratorlocal settingstemp emp5190.txt

  

Java Creating files and directories

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.