The key technical points for creating files and directories are as follows:
<pre name= "code" class= "java" > 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 a file, the creation fails
2, The mkdir method of the file class creates
a table of contents 3, the file class based on the abstract path, and creates a directory based on the abstract path, including the creation of a createtempfile method of the required but Non-existent parent directory 4, and the file class to create a
temporary file. You can make the file name prefix, suffix, and directory where the file resides, and if you do not specify a directory, it is 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
instance demo
Package Book.io;
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 destination file already exists.)
");
return false; } if (Destfilename.endswith (File.separator)) {System.out.println ("Create a single file + destFileName +" fails, the destination file cannot As 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 S YSTEM.OUT.PRINTLN ("The directory in which the destination file resides does not exist, prepare to create it.")
"); if (!file.getparentfile (). Mkdirs ()) {System.out.println () failed to create the directory in which the destination file resides.
");
return false; }///create target file try {if (File.createnewfile ()) {System.out.println ("create single File "+ destFileName +" succeeded.
");
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 +) succeeded.
");
return true; } else {System.out.println ("Create directory" + destDirName +) failed.
");
return false; } public static string Createtempfile (string prefix, stringsuffix, String dirname) {File tempfile = null; if (dirname = = null) {try{//create temporary file under Default folder Tempfile = File.createtempfile (pre
fix, suffix);
Returns the path to the temporary file return Tempfile.getcanonicalpath ();
catch (IOException e) {e.printstacktrace (); SYSTEM.OUT.PRINTLN ("Create temporary 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 the directory where temporary files reside.")
");
return null; try {//create temporary file under specified directory tempfile = file.createtempfile (prefix, su
Ffix, dir);
return Tempfile.getcanonicalpath ();
catch (IOException e) { E.printstacktrace (); SYSTEM.OUT.PRINTLN ("Create temporary file failed.)
"+ e.getmessage ());
return null; }} public static void Main (string[] args) {//create directory String dirname = "d:/work/temp/t
Emp0/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 < i++) {System.out.println ("created temporary file:" + Createfileutil.createtem
PFile (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:")
+ createfileutil.createtempfile (prefix, suffix, null));
Output result: Create directory D:/WORK/TEMP/TEMP0/TEMP1 successful.
The directory where the destination file is located does not exist and is ready to create it. Create a single file D:/work/temp/temp0/temp1/temp2/tempfile.txt successful。
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 Temporary file created: 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 T creates a temporary file: d:work emp emp0 emp1 emp5177.txt created a temporary file: d:work emp emp0 emp1 The Emp5178.txt created temporary files: d:work emp emp0 EMP1 XT created temporary files: d:work emp emp0 emp1 emp5180.txt created temporary files under the default directory: C:documents and settingsadministratorlocal settingstemp Emp5181.txt a temporary file was created 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 settingsadministratorlocal settingstemp emp5186.txt Temporary files were created under the default directory: C:documents and settingsadministratorlocal settingstemp Emp5187.txt created temporary files 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