To build a folder locally using Java code
Import Java.io.File;
Import java.io.IOException;
Import Org.slf4j.Logger;
Import Org.slf4j.LoggerFactory; /** * @ClassName Createfileutil.java * @Author Lina * @Describtion Create a file directory on a local disk * @Date Date Created: 2017-7-12 Midday 4:06:40 * */public class Createfileutil {private static final Logger Logger = Loggerfactory.getlogger (Createfileu
Til.class);
public static Boolean CreateFile (String destfilename) {File File = new File (destfilename); if (file.exists ()) {Logger.warn ("Create a single file + destFileName +" failed, the destination file already exists.)
");
return false; } if (Destfilename.endswith (File.separator)) {Logger.warn ("Create single file + destFileName +" failed, destination file cannot be 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 Logger.warn ("The directory in which the destination file resides does not exist, prepare to create it.")
");
if (!file.getparentfile (). Mkdirs ()) { Logger.warn ("The directory where the destination file was created failed.)
");
return false; }///create target file try {if (File.createnewfile ()) {Logger.warn () Create a single file "+ destFileName +" succeeded.
");
return true; } else {Logger.warn ("Create a single file + destFileName +" failed.)
");
return false;
} catch (IOException e) {e.printstacktrace (); Logger.warn ("Create a single file + destFileName +" failed.)
"+ e.getmessage ());
return false; } public static Boolean Createdir (String destdirname) {file Dir = new File (Destdirnam
e);
if (dir.exists ()) {Logger.warn ("Create directory" + destDirName + "failed, target directory already exists");
return false;
} if (!destdirname.endswith (File.separator)) {destdirname = destDirName + file.separator; }//Create directory IF (Dir.mkdirs ()) {Logger.warn ("Create directory" + destDirName +) succeeded.
");
return true; } else {Logger.warn ("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.createtempfi
Le (prefix, suffix);
Returns the path to the temporary file return Tempfile.getcanonicalpath ();
catch (IOException e) {e.printstacktrace (); Logger.warn ("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)) {LOgger.warn ("Create temporary file failed, cannot create the directory where temporary files reside.")
");
return null; try {//create temporary file under specified directory tempfile = file.createtempfile (pr
Efix, suffix, dir);
return Tempfile.getcanonicalpath ();
catch (IOException e) {e.printstacktrace (); Logger.warn ("Create temporary file failed.)
"+ e.getmessage ());
return null; }} public static void Main (string[] args) {//create directory String dirname = "C:
/users/admin/desktop/created folder name ";
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++) {//Logger.warn ("Create temporary file:"//+ Createfileutil.createtempfile (prefix, suffix, dirname)); Create a temporary file in the default directory//for (int i = 0; i < i++) {//System.out.println ("in
Temporary files are created under the default directory: "//+ createfileutil.createtempfile (prefix, suffix, null));
// }
}
}