实现文件夹和文件生成
复制代码代码如下 :
패키지 com.gotobus.common;
import java.io.file;
import java.io.fileInputStream;
import java.io.fileoutputStream;
import java.io.ioexception;
import java.io.inputstream;
공개 클래스 jfile {
public static boolean createfile (String destfilename) {
파일 = 새 파일 (destFilename);
if (file.exists ()) {
거짓을 반환합니다.
}
if (destfilename.endswith (file.separator)) {
거짓을 반환합니다.
}
if (! file.getParentFile (). Exists ()) {
if (! file.getParentFile (). mkdirs ()) {
거짓을 반환합니다.
}
}
노력하다 {
if (file.createnewfile ()) {
진실을 반환하십시오.
} 또 다른 {
거짓을 반환합니다.
}
} catch (ioexception e) {
e.printstacktrace ();
거짓을 반환합니다.
}
}
공개 정적 부울 생성물 (String destdirname) {
파일 dir = 새 파일 (destdirname);
if (dir.exists ()) {
거짓을 반환합니다.
}
if (! destdirname.endswith (file.separator)) {
destdirname = destdirname + file.separator;
}
if (dir.mkdirs ()) {
진실을 반환하십시오.
} 또 다른 {
거짓을 반환합니다.
}
}
public static string createTempFile (문자열 접두사, 문자열 접미사, 문자열 dirname) {
파일 tempfile = null;
if (dirname == null) {
노력하다{
tempfile = file.createtempfile (접두사, 접미사);
return tempfile.getCanonicalPath ();
} catch (ioexception e) {
e.printstacktrace ();
널 리턴;
}
} 또 다른 {
파일 dir = 새 파일 (dirname);
if (! dir.exists ()) {
if (! jfile.createdir (dirname)) {
널 리턴;
}
}
노력하다 {
tempfile = file.createtempfile (접두사, 접미사, dir);
return tempfile.getCanonicalPath ();
} catch (ioexception e) {
e.printstacktrace ();
널 리턴;
}
}
}
public static void CopyFile (String OldPath, String NewPath) {
노력하다 {
int bytesum = 0;
int byteread = 0;
파일 OldFile = 새 파일 (OldPath);
if (oldfile.exists ()) {
inputStream instream = 새로운 fileInputStream (OldPath);
fileoutputStream fs = 새 FileOutputStream (newPath);
바이트 [] 버퍼 = 새로운 바이트 [1444];
int 길이;
while ((byteread = instream.read (buffer))! = -1) {
바이트 += 바이트 리드;
fs.Write (버퍼, 0, Byteread);
}
instream.close ();
}
}
캐치 (예외 e) {
e.printstacktrace ();
}
}
}