实现文件夹和文件生成
复制代码代码如下:
paket com.gotobus.common;
impor java.io.file;
impor java.io.fileInputStream;
impor java.io.fileoutputStream;
impor java.io.ioException;
impor java.io.inputstream;
kelas publik jfile {
public static boolean createFile (string destfileName) {
File file = file baru (destfileName);
if (file.exists ()) {
mengembalikan false;
}
if (destfilename.endswith (file.separator)) {
mengembalikan false;
}
if (! File.getParentFile (). Exists ()) {
if (! File.getParentFile (). mkdirs ()) {
mengembalikan false;
}
}
mencoba {
if (file.createNewFile ()) {
Kembali Benar;
} kalau tidak {
mengembalikan false;
}
} catch (ioException e) {
e.printstacktrace ();
mengembalikan false;
}
}
public static boolean createdir (string destdirname) {
File dir = file baru (destdirname);
if (dir.exists ()) {
mengembalikan false;
}
if (! destdirname.endswith (file.separator)) {
destdirname = destdirname + file.separator;
}
if (dir.mkdirs ()) {
Kembali Benar;
} kalau tidak {
mengembalikan false;
}
}
Public Static String CreateTempFile (Prefix String, String Suffix, String Dirname) {
File tempfile = null;
if (dirname == null) {
mencoba{
tempfile = file.createTempFile (awalan, sufiks);
kembalikan tempfile.getCanonicalPath ();
} catch (ioException e) {
e.printstacktrace ();
kembali nol;
}
} kalau tidak {
File dir = file baru (dirname);
if (! dir.exists ()) {
if (! jfile.createdir (dirname)) {
kembali nol;
}
}
mencoba {
tempfile = file.createTempFile (awalan, sufiks, dir);
kembalikan tempfile.getCanonicalPath ();
} catch (ioException e) {
e.printstacktrace ();
kembali nol;
}
}
}
public static void copyFile (String oldpath, string newPath) {
mencoba {
int bytesum = 0;
int byteread = 0;
File oldfile = file baru (oldpath);
if (oldfile.exists ()) {
InputStream instream = FileInputStream baru (oldpath);
FileOutputStream fs = baru fileOutputStream (newPath);
byte [] buffer = byte baru [1444];
panjang int;
while ((byteread = instream.read (buffer))! = -1) {
bytesum += byteread;
fs.write (buffer, 0, byteread);
}
instream.close ();
}
}
Catch (Exception e) {
e.printstacktrace ();
}
}
}