实现文件夹和文件生成
复制代码代码如下:
แพ็คเกจ com.gotobus.Common;
นำเข้า Java.io.File;
นำเข้า Java.io.FileInputStream;
นำเข้า Java.io.FileOutputStream;
นำเข้า java.io.ioException;
นำเข้า Java.io.InputStream;
คลาสสาธารณะ JFile {
Public Static Boolean CreateFile (String DestFileName) {
ไฟล์ไฟล์ = ไฟล์ใหม่ (destFileName);
if (file.exists ()) {
กลับเท็จ;
-
if (destFileName.endswith (file.separator)) {
กลับเท็จ;
-
if (! file.getParentFile (). มีอยู่ ()) {
if (! file.getParentFile (). mkdirs ()) {
กลับเท็จ;
-
-
พยายาม {
if (file.createnewfile ()) {
กลับมาจริง;
} อื่น {
กลับเท็จ;
-
} catch (ioexception e) {
E.PrintStackTrace ();
กลับเท็จ;
-
-
Public Static Boolean CreateDir (String DestDirName) {
ไฟล์ dir = ไฟล์ใหม่ (destDirName);
if (dir.exists ()) {
กลับเท็จ;
-
if (! destDirName.endswith (file.Sparator)) {
destDirName = destDirName + file.Sparator;
-
if (dir.mkdirs ()) {
กลับมาจริง;
} อื่น {
กลับเท็จ;
-
-
String String สาธารณะ createTempfile (คำนำหน้าสตริง, สตริงต่อท้าย, สตริง dirname) {
ไฟล์ tempfile = null;
if (dirname == null) {
พยายาม{
tempfile = file.createTempfile (คำนำหน้า, ต่อท้าย);
return tempfile.getCanonicalPath ();
} catch (ioexception e) {
E.PrintStackTrace ();
คืนค่า null;
-
} อื่น {
ไฟล์ dir = ไฟล์ใหม่ (ชื่อ dirname);
ถ้า (! dir.exists ()) {
if (! jfile.createdir (dirname)) {
คืนค่า null;
-
-
พยายาม {
tempfile = file.createTempfile (คำนำหน้า, คำต่อท้าย, dir);
return tempfile.getCanonicalPath ();
} catch (ioexception e) {
E.PrintStackTrace ();
คืนค่า null;
-
-
-
โมฆะสาธารณะคงที่ Publ
พยายาม {
int bytesum = 0;
int byteread = 0;
ไฟล์ oldfile = ไฟล์ใหม่ (oldpath);
if (oldfile.exists ()) {
InputStream Instream = ใหม่ FileInputStream (oldPath);
FileOutputStream FS = ใหม่ FileOutputStream (newPath);
ไบต์ [] บัฟเฟอร์ = ไบต์ใหม่ [1444];
ความยาว int;
ในขณะที่ ((byteread = stream.read (บัฟเฟอร์))! = -1) {
Bytesum += byTeread;
fs.write (บัฟเฟอร์, 0, byteread);
-
Instream.close ();
-
-
จับ (ข้อยกเว้น e) {
E.PrintStackTrace ();
-
-
-