java 檔案操作

2021-06-19 23:54:11 字數 2856 閱讀 2166

public class documentutil else else  

copyfinished = this.copyfolder(startfile, desfile);

} return copyfinished;

} // 顯示目錄下所有檔案ming

public static file getdocuments(string path)

// 新建資料夾

public static boolean newfile(string path)

else

}/**

* 此方法為複製單個檔案,如果複製多個檔案可以遞迴呼叫

*/

private boolean copysinglefile(file startsinglefile, file dessinglefile)

//重新整理緩衝區

singledataoutputstream.flush();

} catch (exception e) finally catch (exception e)

} //判斷原始檔和目標檔案大小是否相同,如果相同證明複製成功

if (startsinglefile.length() == dessinglefile.length())

rightcopy = true;

else

rightcopy = false;

return rightcopy;

} public static void format(outputstream os,document doc)throws exception

public boolean copyfolder(file startfolder, file desfolder)

/**

* 複製資料夾函式,此函式是個遞迴,會複製資料夾下的所有檔案

* * @param recfilefolder

* = 需要拷貝的資料夾或子資料夾

* @param recdesfolder

* = 拷貝的目的資料夾或子資料夾,

* @return = true表示成功, false表示失敗

*/

private boolean recursioncopy(file recfilefolder, file recdesfolder) else

} return true;

} /**

* 此函式是為了得到目的資料夾的位址,

* 如:源資料夾為:d:/addme/text (其中text資料夾下有另乙個資料夾 second : d:/addme/text/second)

* 目標位置為:e:/aa/text

* 那麼此second資料夾在目標位址的位置就是 e:/aa/text/second

* 此方法中 startfolderpath = d:/addme/text (源資料夾) ;

* desfolderpath = e:/addme/text (目標位置);

* currentfilepath = d:/addme/text/second(需要複製的子資料夾)

* 返回值為: e:/addme/text/second

*/

private string getsubfilepath(string startfolderpath, string desfolderpath,

string currentfilepath)

/**

* * @param path 檔案路徑

* @param comment 檔案中用於注釋的符號

* @return

*/

public static string readfile(string path,string comment)

str = str+ tempstring+br;

} reader.close();

} catch (ioexception e) finally catch (ioexception e1)

} }

return str;

} public static void writetofile(string filepath, string sets) throws ioexception

public static void renamefile(string path,string newname) throws ioexception

string rootpath = oldfile.getparent();

file newfile = new file(rootpath + file.separator + newname);

if (oldfile.renameto(newfile))

else

} public static boolean isdocumentexsist(string path,string filename)

} }

} //return list;

} /**

* 判斷路徑是否存在

* @param path

* @return

*/

public static boolean ispathexsist(string path)

}

java 檔案操作

複製檔案 param fromfilepath string 目標檔案 param tofilepath string 儲存檔案的路徑 return boolean 複製成功 true 出錯 false public static boolean copyfile string fromfilepa...

Java 檔案操作

private jfilechooser chooser filenameextensionfilter filter new filenameextensionfilter allowed file txt jar chooser.setfilefilter filter intvalue cho...

java檔案操作

檔案的建立 檢查與刪除 string path request.getrealpath file f new file path,file.txt if f.exists else 目錄的建立 檢查與刪除 string path request.getrealpath path path sub 將...