Copy Code code as follows:
Package com.xhkj.util;
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. Destination file already exists! ");
return false;
}
if (Destfilename.endswith (File.separator)) {
System.out.println ("Create a single file + destFileName +" failed, target cannot be a directory!) ");
return false;
}
if (!file.getparentfile (). exists ()) {
System.out.println (" Destination file path does not exist, ready to create ... ");
if (!file.getparentfile (). Mkdirs ()) {
SYSTEM.OUT.PRINTLN ("The directory where the directory file was created failed!") ");
return false;
}
}
//Create destination 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! ");
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 a single directory
if (Dir.mkdirs ()) {
SYSTEM.OUT.PRINTLN ("Create directory" + destDirName + "Success!") ");
return true;
} else {
SYSTEM.OUT.PRINTLN ("Create directory" + destDirName + "Success!") ");
return false;
}
}
public static string Createtempfile (string prefix, string suffix, string dirname) {
File tempfile = null;
try{
if (dirname = = null) {
Create a temporary file under the default folder
Tempfile = file.createtempfile (prefix, suffix);
return Tempfile.getcanonicalpath ();
}
else {
File dir = new file (dirname);
If the directory in which the temporary file resides does not exist, first create
if (!dir.exists ()) {
if (! Createfileutil.createdir (dirname)) {
SYSTEM.OUT.PRINTLN ("Create temporary file failed, cannot create temporary file directory!") ");
return null;
}
}
Tempfile = file.createtempfile (prefix, suffix, 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 a table of contents
String dirname = "C:/test/test0/test1";
Createfileutil.createdir (dirname);
Create a file
String fileName = dirname + "/test2/testfile.txt";
Createfileutil.createfile (FileName);
Create temporary files
String prefix = "temp";
String suffix = ". txt";
for (int i = 0; i < i++) {
SYSTEM.OUT.PRINTLN ("Created temporary file:" + createfileutil.createtempfile (prefix, suffix, dirname));
}
}
}