实现文件夹和文件生成
复制代码代码如下:
paquete com.gotobus.common;
import java.io.file;
import java.io.fileInputStream;
import java.io.fileOutputStream;
import java.io.ioException;
import java.io.inputstream;
clase pública JFile {
Public static boolean createFile (string DestFileName) {
Archivo archivo = nuevo archivo (destfileName);
if (file.exists ()) {
devolver falso;
}
if (destfilename.endswith (file.separator)) {
devolver falso;
}
if (! file.getParentFile (). Exists ()) {
if (! file.getParentFile (). mkdirs ()) {
devolver falso;
}
}
intentar {
if (file.createenewfile ()) {
devolver verdadero;
} demás {
devolver falso;
}
} Catch (ioException e) {
E.PrintStackTrace ();
devolver falso;
}
}
public static boolean creatirir (string DestDirname) {
Archivo dir = nuevo archivo (destdirname);
if (dir.exists ()) {
devolver falso;
}
if (! Destdirname.endswith (file.separator)) {
DestDirname = DestDirname + File.separator;
}
if (dir.mkdirs ()) {
devolver verdadero;
} demás {
devolver falso;
}
}
public static string createTempfile (prefijo de cadena, sufijo de cadena, string dirname) {
Archivo tempfile = null;
if (dirname == null) {
intentar{
tempfile = file.createTempfile (prefijo, sufijo);
return tempfile.getCanonicalPath ();
} Catch (ioException e) {
E.PrintStackTrace ();
regresar nulo;
}
} demás {
Archivo dir = nuevo archivo (dirname);
if (! dir.exists ()) {
if (! jfile.createDir (dirname)) {
regresar nulo;
}
}
intentar {
tempfile = file.createTempfile (prefijo, sufijo, dir);
return tempfile.getCanonicalPath ();
} Catch (ioException e) {
E.PrintStackTrace ();
regresar nulo;
}
}
}
public static void copyfile (String OldPath, String Newpath) {
intentar {
int bytesum = 0;
int byteread = 0;
Archivo OldFile = nuevo archivo (OldPath);
if (oldFile.exists ()) {
InputStream InSteam = new FileInputStream (OldPath);
FileOutputStream fs = new FileOutputStream (NewPath);
byte [] buffer = new Byte [1444];
int longitud;
while ((byteread = instramp.read (buffer))! = -1) {
bytesum += byteread;
fs.write (buffer, 0, byteread);
}
Intileam.close ();
}
}
Catch (excepción e) {
E.PrintStackTrace ();
}
}
}