Create a folder locally (multi-tier directory) using Java code and create a file directory on the local disk

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

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));

 //        }  
    } 

}


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.