实现文件夹和文件生成
复制代码代码如下:
pacote com.gotobus.common;
importar java.io.file;
importar java.io.fileInputStream;
importar java.io.fileOutputStream;
importar java.io.ioException;
importar java.io.inputStream;
classe pública jfile {
public static boolean createfile (string destfileName) {
Arquivo de arquivo = novo arquivo (destfilename);
if (file.exists ()) {
retornar falso;
}
if (destfilename.endswith (file.separator)) {
retornar falso;
}
if (! file.getParentFile (). Exist ()) {
if (! file.getparentfile (). mkdirs ()) {
retornar falso;
}
}
tentar {
if (file.createnewfile ()) {
retornar true;
} outro {
retornar falso;
}
} catch (ioexception e) {
E.PrintStackTrace ();
retornar falso;
}
}
public static boolean CreatedIr (String DestdirName) {
Arquivo Dir = novo arquivo (DestdirName);
if (dir.exists ()) {
retornar falso;
}
if (! Destdirname.endswith (file.separator)) {
DestdirName = DestdirName + File.separator;
}
if (dir.mkdirs ()) {
retornar true;
} outro {
retornar falso;
}
}
public static string createTeTempFile (prefixo de string, sufixo de string, string dirname) {
Arquivo tempfile = null;
if (Dirname == null) {
tentar{
tempfile = file.createTempFile (prefixo, sufixo);
retornar tempfile.getCanonicalPath ();
} catch (ioexception e) {
E.PrintStackTrace ();
retornar nulo;
}
} outro {
Arquivo Dir = novo arquivo (Dirname);
if (! Dir.Exists ()) {
if (! jfile.createdir (Dirname)) {
retornar nulo;
}
}
tentar {
tempfile = file.createTempFile (prefixo, sufixo, dir);
retornar tempfile.getCanonicalPath ();
} catch (ioexception e) {
E.PrintStackTrace ();
retornar nulo;
}
}
}
public static void copyfile (string OldPath, string newPath) {
tentar {
int byTesum = 0;
int byteread = 0;
Arquivo OldFile = novo arquivo (OldPath);
if (OldFile.Exists ()) {
InputStream InstrtEam = new FileInputStream (OldPath);
FileOutputStream fs = new FileOutputStream (newPath);
byte [] buffer = novo byte [1444];
comprimento int;
while ((byteread = Instream.read (buffer))! = -1) {
byTesum += byteread;
fs.write (buffer, 0, byteread);
}
Instruam.close ();
}
}
Catch (Exceção e) {
E.PrintStackTrace ();
}
}
}