Java Create folder and file instance code _java

Source: Internet
Author: User
Tags create directory

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

}

}

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.