实现文件夹和文件生成
复制代码代码如下:
пакет 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) {
File file = new File (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 CreativeIr (String destdirname) {
File dir = new File (destdirName);
if (dir.exists ()) {
вернуть ложь;
}
if (! destdirname.endswith (file.separator)) {
destdirname = destdirname + file.separator;
}
if (dir.mkdirs ()) {
вернуть истину;
} еще {
вернуть ложь;
}
}
Public Static String CreateMpeMpile (String Prefix, String Suffix, String Dirname) {
File tempfile = null;
if (dirname == null) {
пытаться{
tempfile = file.createtempfile (префикс, суффикс);
return tempfile.getCanonicalPath ();
} catch (ioException e) {
e.printstacktrace ();
вернуть ноль;
}
} еще {
Файл dir = new File (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;
File oldfile = новый файл (OldPath);
if (oldfile.exists ()) {
Inputstream instriam = new FileInputStream (OldPath);
FileOutputStream fs = new FileOutputStream (newPath);
byte [] buffer = new Byte [1444];
int длина;
while ((byteread = instream.read (buffer))! = -1) {
байтесум += byteread;
fs.write (буфер, 0, Byteread);
}
instream.close ();
}
}
поймать (исключение e) {
e.printstacktrace ();
}
}
}