实现文件夹和文件生成
复制代码代码如下:
パッケージ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()){
falseを返します。
}
if(destfilename.endswith(file.separator)){
falseを返します。
}
if(!file.getParentFile()。exists()){
if(!file.getParentFile()。mkdirs()){
falseを返します。
}
}
試す {
if(file.createNewFile()){
trueを返します。
} それ以外 {
falseを返します。
}
} catch(ioException e){
e.printstacktrace();
falseを返します。
}
}
public static boolean createdir(string destdirname){
ファイルdir = new file(destdirname);
if(dir.exists()){
falseを返します。
}
if(!destdirname.endswith(file.separator)){
destdirname = destdirname + file.separator;
}
if(dir.mkdirs()){
trueを返します。
} それ以外 {
falseを返します。
}
}
public static string createTempfile(文字列prefix、string suffix、string dirname){
ファイルtempfile = null;
if(dirname == null){
試す{
tempfile = file.createTempfile(prefix、suffix);
return tempfile.getCanonicalPath();
} catch(ioException e){
e.printstacktrace();
nullを返します。
}
} それ以外 {
ファイルdir = new file(dirname);
if(!dir.exists()){
if(!jfile.createdir(dirname)){
nullを返します。
}
}
試す {
tempfile = file.createTempfile(prefix、suffix、dir);
return tempfile.getCanonicalPath();
} catch(ioException e){
e.printstacktrace();
nullを返します。
}
}
}
public static void copyfile(string oldpath、string newpath){
試す {
int bytesum = 0;
int byteread = 0;
ファイルoldfile = new file(oldpath);
if(oldfile.exists()){
inputStream instream = new FileInputStream(OldPath);
fileoutputStream fs = new fileoutputStream(newPath);
byte [] buffer = new byte [1444];
int length;
while((byteread = instream.read(buffer))!= -1){
bytesum += byteread;
fs.write(buffer、0、byteread);
}
instream.close();
}
}
catch(例外e){
e.printstacktrace();
}
}
}