Create a folder in Java

Source: Internet
Author: User
Package test; import Java. io. file; import Java. io. ioexception; public class createfiletest {/*** create a single file * @ Param destfilename file name * @ return returns true if the file is created successfully; otherwise, returns false */public static Boolean createfile (string destfilename) {file = new file (destfilename); If (file. exists () {system. out. println ("Creating a single file" + destfilename + "failed. The target file already exists! "); Return false;} If (destfilename. endswith (file. separator) {system. out. println ("Creating a single file" + destfilename + "failed. The target cannot be a directory! "); Return false;} If (! File. getparentfile (). exists () {system. Out. println ("The path of the target file does not exist. You are about to create a file... "); If (! File. getparentfile (). mkdirs () {system. Out. println ("An error occurred while creating the directory where the directory file is located! "); Return false ;}}// create the target file try {If (file. createnewfile () {system. out. println ("Creating a single file" + destfilename + "successful! "); Return true;} else {system. Out. println (" Creating a single file "+ destfilename +" failed! "); Return false ;}} catch (ioexception e) {e. printstacktrace (); system. Out. println (" Creating a single file "+ destfilename +" failed! "); Return false;}/*** create directory * @ Param destdirname target directory name * @ return returns true if the directory is created successfully, otherwise, false */public static Boolean createdir (string destdirname) {file dir = new file (destdirname); If (dir. exists () {system. out. println ("creating directory" + destdirname + "failed. The 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 ("creating directory" + destdirname + "successful! "); Return true;} else {system. Out. println (" create directory "+ destdirname +" successful! "); Return false ;}} /*** create a temporary file * @ Param prefix the prefix of the temporary file * @ Param suffix the suffix of the temporary file * @ Param dirname the directory where the temporary file is located. If the input is null, then, create a temporary file * @ return under the user's document directory. After the temporary file is successfully created, return the standard path name string of the abstract path. Otherwise, return NULL */public static string createtempfile (string prefix, string suffix, string dirname) {file tempfile = NULL; try {If (dirname = NULL) {// create a temporary file tempfile = file in the default folder. createtempfile (prefix, suffix); Return tempfile. getcanonicphosphatase Ath ();} else {file dir = new file (dirname); // If the directory of the temporary file does not exist, create if (! Dir. exists () {If (! Createfiletest. createdir (dirname) {system. Out. println ("Creating a temporary file failed. The directory where the temporary file cannot be created! "); Return NULL ;}} tempfile = file. createtempfile (prefix, suffix, DIR); return tempfile. getCanonicalPath () ;}} catch (ioexception e) {e. printstacktrace (); system. out. println ("failed to create temporary file" + E. getmessage (); return NULL ;}} public static void main (string [] ARGs) {// create the directory string dirname = "D:/test/test0/test1 "; createfiletest. createdir (dirname); // create the file string filename = dirname + "/Test2/testfile.txt"; createfiletest. createfile (filename); // create a temporary file string prefix = "Temp"; string suffix = ". TXT "; for (INT I = 0; I <10; I ++) {system. out. println ("temporary file created:" + createfiletest. createtempfile (prefix, suffix, dirname ));}}}

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.