JAVA對檔案和資料夾的操作

2021-08-30 11:50:07 字數 3615 閱讀 9899

[b]讀取文字檔案內容[/b]

[b]新建目錄[/b]

/**

* 新建目錄

* @param folderpath 目錄

* @return 返回目錄建立後的路徑

*/public string createfolder(string folderpath)

return txt;

}

[b]多級目錄建立[/b]

/**

* 多級目錄建立

* @param folderpath 準備要在本級目錄下建立新目錄的目錄路徑 例如 c:myf

* @param paths 無限級目錄引數,各級目錄以單數線區分 例如 a|b|c

* @return 返回建立檔案後的路徑 例如 c:myfac

*/public string createfolders(string folderpath,string paths)else

}return txts;

}

[b]新建檔案[/b]

/**

* 新建檔案

* @param filepathandname 文字檔案完整絕對路徑及檔名

* @param filecontent 文字檔案內容

* @return

*/public void createfile(string filepathandname,string filecontent)

filewriter resultfile = new filewriter(myfilepath);

printwriter myfile = new printwriter(resultfile);

string strcontent = filecontent;

myfile.println(strcontent);

myfile.close();

resultfile.close();

}catch (ioexception e)

}

[b]有編碼方式的檔案建立[/b]

/**

* 有編碼方式的檔案建立

* @param filepathandname 文字檔案完整絕對路徑及檔名

* @param filecontent 文字檔案內容

* @param encoding 編碼方式 例如 gbk 或者 utf-8

* @return

*/public void createfile(string filepathandname, string filecontent, string encoding)

printwriter myfile = new printwriter(myfilepath,encoding);

string strcontent = filecontent;

myfile.println(strcontent);

myfile.close();

}catch (exception e)

}

[b]刪除檔案[/b]

/**

* 刪除檔案

* @param filepathandname 文字檔案完整絕對路徑及檔名

* @return boolean 成功刪除返回true遭遇異常返回false

*/public boolean delfile(string filepathandname) else

}catch (exception e)

return bea;

}

[b]刪除資料夾[/b]

/**

* 刪除資料夾

* @param folderpath 資料夾完整絕對路徑

* @return

*/public void delfolder(string folderpath)

catch (exception e)

}

[b]刪除指定資料夾下所有檔案[/b]

/**

* 刪除指定資料夾下所有檔案

* @param path 資料夾完整絕對路徑

* @return

* @return

*/public boolean delallfile(string path)

if (!file.isdirectory())

string templist = file.list();

file temp = null;

for (int i = 0; i < templist.length; i++) else

if (temp.isfile())

if (temp.isdirectory())

}return bea;

}

[b]複製單個檔案[/b]

/**

* 複製單個檔案

* @param oldpathfile 準備複製的檔案源

* @param newpathfile 拷貝到新絕對路徑帶檔名

* @return

*/public void copyfile(string oldpathfile, string newpathfile)

instream.close();

}}catch (exception e)

}

[b]複製整個資料夾的內容[/b]

/**

* 複製整個資料夾的內容

* @param oldpath 準備拷貝的目錄

* @param newpath 指定絕對路徑的新目錄

* @return

*/public void copyfolder(string oldpath, string newpath) else

if(temp.isfile())

output.flush();

output.close();

input.close();

}if(temp.isdirectory())

}}catch (exception e)

}

[b]移動檔案[/b]

/**

* 移動檔案

* @param oldpath

* @param newpath

* @return

*/public void movefile(string oldpath, string newpath)

[b]移動目錄[/b]

/**

* 移動目錄

* @param oldpath

* @param newpath

* @return

*/public void movefolder(string oldpath, string newpath)

對檔案和資料夾操作的簡單函式

python中對檔案 資料夾 檔案操作函式 的操作需要涉及到os模組和shutil模組。得到當前工作目錄,即當前python指令碼工作的目錄路徑 os.getcwd 返回指定目錄下的所有檔案和目錄名 os.listdir 函式用來刪除乙個檔案 os.remove 刪除多個目錄 os.removedi...

python檔案和資料夾操作

python中對檔案 資料夾 檔案操作函式 的操作需要涉及到os模組和shutil模組。得到當前工作目錄,即當前python指令碼工作的目錄路徑 os.getcwd 返回指定目錄下的所有檔案和目錄名 os.listdir 函式用來刪除乙個檔案 os.remove 刪除多個目錄 os.removedi...

Perl 檔案和資料夾操作

作為一門程式語言,perl有著強大的檔案和資料夾操作功能。perl利用檔案控制代碼 file handle 和資料夾控制代碼 dir handle 與作業系統進行輸入輸出連線。檔案和資料夾控制代碼一般使用大寫子母或指定標量以示特殊。檔案開啟語句及說明 一般加入die函式做開啟失敗的提示。my fil...