实现文件夹和文件生成
复制代码代码如下:
حزمة com.gotobus.common ؛
استيراد java.io.file ؛
استيراد java.io.fileInputStream ؛
استيراد java.io.fileOutputStream ؛
استيراد java.io.ioException ؛
استيراد java.io.inputstream ؛
الطبقة العامة jfile {
{static boolean createfile (سلسلة 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 () ؛
العودة كاذبة
}
}
BOOLEAN STATIC STATIC CRATER (سلسلة DESTDIRNAME) {
ملف dir = ملف جديد (destDirName) ؛
if (dir.exists ()) {
العودة كاذبة
}
if (! destdirname.endswith (file.separator)) {
destdirname = destdirname + file.separator ؛
}
if (dir.mkdirs ()) {
العودة صحيح.
} آخر {
العودة كاذبة
}
}
السلسلة الثابتة العامة CreateTempFile (بادئة السلسلة ، لاحقة السلسلة ، سلسلة dirname) {
ملف tempfile = null ؛
if (dirname == null) {
يحاول{
tempfile = file.createTempFile (بادئة ، لاحقة) ؛
إرجاع tempfile.getCanonicalPath () ؛
} catch (ioException e) {
E.PrintStackTrace () ؛
العودة لاغية.
}
} آخر {
ملف dir = ملف جديد (dirname) ؛
if (! dir.exists ()) {
if (! jfile.createdir (dirname)) {
العودة لاغية.
}
}
يحاول {
tempfile = file.createTempFile (بادئة ، لاحقة ، dir) ؛
إرجاع 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) ؛
Byte [] Buffer = New Byte [1444] ؛
طول int
بينما ((byteread = instream.read (buffer))! = -1) {
bytesum += byteread ؛
fs.write (العازلة ، 0 ، byteread) ؛
}
instream.close () ؛
}
}
استثناء (استثناء ه) {
E.PrintStackTrace () ؛
}
}
}