实现文件夹和文件生成
复制代码代码如下:
package com.gotobus.common;
Importer java.io.file;
import java.io.fileInputStream;
Importer java.io.fileOutputStream;
Importer java.io.ioException;
import java.io.inputStream;
classe publique jfile {
public static boolean créeFile (string destfilename) {
File file = new File (destFileName);
if (file.exists ()) {
retourne false;
}
if (destFileName.endswith (file.separator)) {
retourne false;
}
if (! file.getParentFile (). existant ()) {
if (! file.getParentFile (). mkDirs ()) {
retourne false;
}
}
essayer {
if (file.createenewfile ()) {
Retour Vrai;
} autre {
retourne false;
}
} catch (ioexception e) {
e.printStackTrace ();
retourne false;
}
}
public static boolean crééir (string destdirname) {
File dir = nouveau fichier (destdirName);
if (dir.exists ()) {
retourne false;
}
if (! destdirname.endswith (file.separator)) {
destdirname = destdirname + file.separator;
}
if (dir.mkDirs ()) {
Retour Vrai;
} autre {
retourne false;
}
}
public static string createTEmpFile (String Prefix, String suffixe, String dirname) {
Fichier tempfile = null;
if (dirname == null) {
essayer{
tempfile = file.createTempFile (préfixe, suffixe);
Retour Tempfile.getCanonicalPath ();
} catch (ioexception e) {
e.printStackTrace ();
retourner null;
}
} autre {
Fichier dir = nouveau fichier (dirname);
if (! dir.exists ()) {
if (! jfile.createdir (dirname)) {
retourner null;
}
}
essayer {
tempfile = file.createTempFile (préfixe, suffixe, dir);
Retour Tempfile.getCanonicalPath ();
} catch (ioexception e) {
e.printStackTrace ();
retourner null;
}
}
}
public static void copyfile (string oldpath, string newpath) {
essayer {
int bytesum = 0;
int bytereread = 0;
File oldfile = nouveau fichier (oldpath);
if (oldfile.exists ()) {
InputStream InsideRam = new FileInputStream (OldPath);
FileoutputStream fs = new FileOutputStream (newPath);
octet [] tampon = nouveau octet [1444];
longueur int;
while ((byteread = insiteam.read (tampon))! = -1) {
bytesum + = byteread;
fs.write (tampon, 0, byteread);
}
instream.close ();
}
}
attraper (exception e) {
e.printStackTrace ();
}
}
}