實現文件夾和文件生成
複製代碼代碼如下:
包com.gotobus.common;
導入java.io.file;
導入java.io.io.fileinputstream;
導入java.io.io.fileoutputstream;
導入java.io.ioexception;
導入java.io.inputstream;
公共類Jfile {
public static boolean createfile(字符串destfileName){
文件文件=新文件(destfileName);
如果(file.exists()){
返回false;
}
if(destfilename.endswith(file.separator)){
返回false;
}
如果(!file.getParentFile()。
如果(!file.getParentFile().mkdirs()){
返回false;
}
}
嘗試 {
if(file.createnewfile()){
返回true;
} 別的 {
返回false;
}
} catch(ioexception e){
e.printstacktrace();
返回false;
}
}
public static boolean createir(字符串destdirname){
文件dir = new文件(destdirname);
如果(dir.exists()){
返回false;
}
如果(!destdirname.endswith(file.separator)){
destdirname = destdirname + file.separator;
}
if(dir.mkdirs()){
返回true;
} 別的 {
返回false;
}
}
公共靜態字符串createTemPfile(字符串前綴,字符串後綴,字符串dirname){
文件tempfile = null;
如果(dirname == null){
嘗試{
tempfile = file.createTemPfile(前綴,後綴);
返回tempfile.getCanonicalPath();
} catch(ioexception e){
e.printstacktrace();
返回null;
}
} 別的 {
文件dir = new File(dirName);
如果(!dir.exists()){
如果(!jfile.createdir(dirname)){
返回null;
}
}
嘗試 {
tempfile = file.createTemPfile(前綴,後綴,dir);
返回tempfile.getCanonicalPath();
} catch(ioexception e){
e.printstacktrace();
返回null;
}
}
}
public static void copyfile(字符串oldpath,string newpath){
嘗試 {
int bytesum = 0;
int byteread = 0;
文件oldfile = new File(oldPath);
if(oldfile.exists()){
InputStream Instream = new FileInputStream(oldPath);
FileOutputStream fs = new FileOutputStream(newPath);
字節[] buffer = new Byte [1444];
int長度;
while(((byteread = instream.read(buffer))!= -1){
字節 += byteread;
fs.Write(Buffer,0,byteread);
}
instream.close();
}
}
捕獲(異常e){
e.printstacktrace();
}
}
}