实现文件夹和文件生成
复制代码代码如下:
包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();
}
}
}