JAVA操作檔案夾

來源:互聯網
上載者:User

 package com.file.test;</p><p>import java.io.File;</p><p>/**<br /> *<br /> * @author xiaxr JAVA操作檔案夾<br /> */</p><p>public class FileServerTest {</p><p>private boolean bool = false;<br />private StringBuffer returnStr = new StringBuffer();</p><p>/**<br /> * 建立檔案夾(不是根據路徑級聯建立,如果目錄的上一級不存在就不能繼續建立)<br /> */<br />public boolean CreateFileServer(String path) {<br />File file = new File(path);<br />// 判斷檔案夾是否存在,不存在就建立新檔案夾<br />if (!file.isDirectory()) {<br />bool = file.mkdir();<br />}<br />return bool;<br />}</p><p>/**<br /> * 建立檔案夾(根據路徑級聯建立,如果目錄的上一級目錄不存在則按路徑建立)<br /> */<br />public boolean CreateFileServer2(String path) {<br />// 根據符號"/"來分隔路徑<br />String[] paths = path.split("/");<br />int length = paths.length;<br />for (int i = 0; i < length; i++) {<br />returnStr.append(paths[i]);<br />File file = new File(returnStr.toString());<br />if (!file.isDirectory()) {<br />bool = file.mkdir();<br />}<br />returnStr.append("/");<br />}<br />return bool;<br />}</p><p>/**<br /> * 刪除指定路徑所有檔案和檔案<br /> */<br />public boolean delAllFile(String path) {<br />File file = new File(path);<br />if (file.exists()) {<br />if(file.isDirectory()){<br />String[] tempList = file.list();<br />File temp = null;<br />for (int i = 0; i < tempList.length; i++) {<br />if (path.endsWith(File.separator)) {<br />temp = new File(path + tempList[i]);<br />} else {<br />temp = new File(path + File.separator + tempList[i]);<br />}<br />if (temp.isFile()) {<br />temp.delete();<br />}<br />if (temp.isDirectory()) {<br />delAllFile(path + "/" + tempList[i]);// 先刪除檔案夾裡面的檔案<br />delFolder(path + "/" + tempList[i]);// 再刪除空檔案夾<br />}<br />}<br />}else if(file.isFile()){<br />file.delete();<br />}<br />bool = true;<br />}<br />return bool;<br />}</p><p>/**<br /> * 刪除檔案夾 param folderPath 檔案夾完整絕對路徑<br /> */<br />public boolean delFolder(String path) {<br />try {<br />delAllFile(path); // 刪除完裡面所有內容<br />File file = new File(path);<br />if(file.isDirectory()){<br />bool = file.delete(); // 刪除空檔案夾<br />}<br />} catch (Exception e) {<br />e.printStackTrace();<br />}<br />return bool;<br />}</p><p>/**<br /> * 刪除檔案夾(如果檔案夾下面有檔案或者子檔案夾時就不會執行)<br /> */<br />public boolean deleteFileServer(String path) {<br />File file = new File(path);<br />// 判斷檔案夾是否存在<br />if (file.isDirectory()) {<br />bool = file.delete();<br />}<br />return bool;<br />}</p><p>/**<br /> * 讀取檔案夾下面所有檔案夾和檔案的內容<br /> */<br />public void readFileServer(String path){<br />File file = new File(path);<br />File[] tempList = file.listFiles();<br />String[] tempStr = file.list();<br />File temp = null;<br />for(int i=0;i<tempList.length;i++){<br />temp = tempList[i];<br />if(temp.isFile()){<br />System.out.println("檔案名稱:"+temp.getName()+" 檔案路徑:"+temp.getAbsolutePath());<br />}else if(temp.isDirectory()){<br />System.out.println("-----------------");<br />readFileServer(path+"/"+tempStr[i]);<br />System.out.println("-----------------");<br />}<br />}<br />}</p><p>/**<br /> * @param args<br /> */<br />public static void main(String[] args) {<br />FileServerTest test = new FileServerTest();</p><p>//System.out.println(test.CreateFileServer("c:/11111/22222/444"));<br />//System.out.println(test.CreateFileServer2("c:/11111/22222/333"));</p><p>//System.out.println(test.deleteFileServer("c:/11111/22222/333"));<br />//System.out.println(test.delFolder("c:/11111/22222/333"));</p><p>//刪除指定路徑所有檔案和檔案<br />//System.out.println(test.delAllFile("c:/11111/22222"));</p><p>test.readFileServer("c:/11111/22222");</p><p>}</p><p>}<br />

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.