Java拷貝文件夾和刪除文件夾代碼實(shí)例
1.文件夾的拷貝
public void copy(File src, File dest) throws IOException {if (dest.isFile()) {throw new RuntimeException(dest + '不是文件夾');}File file = new File(dest.getPath() + '/' + src.getName());if (src.isFile()) {FileInputStream fis = new FileInputStream(src);BufferedInputStream bis = new BufferedInputStream(fis);FileOutputStream fos = new FileOutputStream(file);BufferedOutputStream bos = new BufferedOutputStream(fos);byte[] bytes = new byte[1024];int len;while ((len = bis.read(bytes)) != -1) {bos.write(bytes, 0, len);}bis.close();fis.close();bos.close();fos.close();} else if(src.isDirectory()){file.mkdir();File[] files = src.listFiles();for (File file2 : files) {copy(file2, dest);}}}
2. 刪除整個(gè)文件夾:
public void delete(File dir) {if (dir.isDirectory()) {File[] files = dir.listFiles();for (File file : files) {delete(file);}}dir.delete();}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. HTTP協(xié)議常用的請(qǐng)求頭和響應(yīng)頭響應(yīng)詳解說(shuō)明(學(xué)習(xí))2. CSS清除浮動(dòng)方法匯總3. HTML DOM setInterval和clearInterval方法案例詳解4. HTML5實(shí)戰(zhàn)與剖析之觸摸事件(touchstart、touchmove和touchend)5. Vue如何使用ElementUI對(duì)表單元素進(jìn)行自定義校驗(yàn)及踩坑6. XML入門的常見(jiàn)問(wèn)題(三)7. XML在語(yǔ)音合成中的應(yīng)用8. jscript與vbscript 操作XML元素屬性的代碼9. HTML5 Canvas繪制圖形從入門到精通10. 不要在HTML中濫用div
