Implementing folder and File generation
Copy Code code as follows:
Package Com.gotobus.common;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
public class JFile {
public static Boolean CreateFile (String destfilename) {
File File = new file (destfilename);
if (file.exists ()) {
return false;
}
if (Destfilename.endswith (File.separator)) {
return false;
}
if (!file.getparentfile (). exists ()) {
if (!file.getparentfile (). Mkdirs ()) {
return false;
}
}
try {
if (File.createnewfile ()) {
return true;
} else {
return false;
}
catch (IOException e) {
E.printstacktrace ();
return false;
}
}
public static Boolean Createdir (String destdirname) {
File dir = new file (destdirname);
if (dir.exists ()) {
return false;
}
if (!destdirname.endswith (File.separator)) {
destDirName = destDirName + file.separator;
}
if (Dir.mkdirs ()) {
return true;
} else {
return false;
}
}
public static string Createtempfile (string prefix, string suffix, string dirname) {
File tempfile = null;
if (dirname = = null) {
try{
Tempfile = file.createtempfile (prefix, suffix);
return Tempfile.getcanonicalpath ();
catch (IOException e) {
E.printstacktrace ();
return null;
}
} else {
File dir = new file (dirname);
if (!dir.exists ()) {
if (! Jfile.createdir (dirname)) {
return null;
}
}
try {
Tempfile = file.createtempfile (prefix, suffix, dir);
return Tempfile.getcanonicalpath ();
catch (IOException e) {
E.printstacktrace ();
return null;
}
}
}
public static void CopyFile (String oldpath, String NewPath) {
try {
int bytesum = 0;
int byteread = 0;
File Oldfile = new file (OldPath);
if (oldfile.exists ()) {
InputStream instream = new FileInputStream (OldPath);
FileOutputStream fs = new FileOutputStream (NewPath);
byte[] buffer = new byte[1444];
int length;
while ((Byteread = instream.read (buffer))!=-1) {
Bytesum + = Byteread;
Fs.write (buffer, 0, byteread);
}
Instream.close ();
}
}
catch (Exception e) {
E.printstacktrace ();
}
}
}