package com.file.test;</p><p>import java.io.BufferedReader;<br />import java.io.File;<br />import java.io.FileInputStream;<br />import java.io.FileOutputStream;<br />import java.io.FileWriter;<br />import java.io.IOException;<br />import java.io.InputStream;<br />import java.io.InputStreamReader;<br />import java.io.PrintWriter;<br />import java.util.StringTokenizer;</p><p>/**<br /> *<br /> * @author xiaxr<br /> *<br /> */</p><p>public class FileOperate {<br /> private String message;<br /> public FileOperate() {<br /> }<br /> /**<br /> * 讀取文字檔內容<br /> * @param filePathAndName 帶有完整絕對路徑的檔案名稱<br /> * @param encoding 文字檔開啟的編碼方式<br /> * @return 返迴文本檔案的內容<br /> */<br /> public String readTxt(String filePathAndName,String encoding) throws IOException{<br /> encoding = encoding.trim();<br /> StringBuffer str = new StringBuffer("");<br /> String st = "";<br /> try{<br /> FileInputStream fs = new FileInputStream(filePathAndName);<br /> InputStreamReader isr;<br /> if(encoding.equals("")){<br /> isr = new InputStreamReader(fs);<br /> }else{<br /> isr = new InputStreamReader(fs,encoding);<br /> }<br /> BufferedReader br = new BufferedReader(isr);<br /> try{<br /> String data = "";<br /> while((data = br.readLine())!=null){<br /> str.append(data+" ");<br /> }<br /> }catch(Exception e){<br /> str.append(e.toString());<br /> }<br /> st = str.toString();<br /> }catch(IOException es){<br /> st = "";<br /> }<br /> return st;<br /> }</p><p> /**<br /> * 建立目錄<br /> * @param folderPath 目錄<br /> * @return 返回目錄建立後的路徑<br /> */<br /> public String createFolder(String folderPath) {<br /> String txt = folderPath;<br /> try {<br /> java.io.File myFilePath = new java.io.File(txt);<br /> txt = folderPath;<br /> if (!myFilePath.exists()) {<br /> myFilePath.mkdir();<br /> }<br /> }<br /> catch (Exception e) {<br /> message = "建立目錄操作出錯";<br /> }<br /> return txt;<br /> }</p><p> /**<br /> * 多級目錄建立<br /> * @param folderPath 準備要在本級目錄下建立新目錄的目錄路徑 例如 c:myf<br /> * @param paths 無限級目錄參數,各級目錄以單數線區分 例如 a|b|c<br /> * @return 返回建立檔案後的路徑 例如 c:myfac<br /> */<br /> public String createFolders(String folderPath, String paths){<br /> String txts = folderPath;<br /> try{<br /> String txt;<br /> txts = folderPath;<br /> StringTokenizer st = new StringTokenizer(paths,"|");<br /> for(int i=0; st.hasMoreTokens(); i++){<br /> txt = st.nextToken().trim();<br /> if(txts.lastIndexOf("/")!=-1){<br /> txts = createFolder(txts+txt);<br /> }else{<br /> txts = createFolder(txts+txt+"/");<br /> }<br /> }<br /> }catch(Exception e){<br /> message = "建立目錄操作出錯!";<br /> }<br /> return txts;<br /> }</p><p> /**<br /> * 建立檔案<br /> * @param filePathAndName 文字檔完整絕對路徑及檔案名稱<br /> * @param fileContent 文字檔內容<br /> * @return<br /> */<br /> public void createFile(String filePathAndName, String fileContent) {</p><p> try {<br /> String filePath = filePathAndName;<br /> filePath = filePath.toString();<br /> File myFilePath = new File(filePath);<br /> if (!myFilePath.exists()) {<br /> myFilePath.createNewFile();<br /> }<br /> FileWriter resultFile = new FileWriter(myFilePath);<br /> PrintWriter myFile = new PrintWriter(resultFile);<br /> String strContent = fileContent;<br /> myFile.println(strContent);<br /> myFile.close();<br /> resultFile.close();<br /> }<br /> catch (Exception e) {<br /> message = "建立檔案操作出錯";<br /> }<br /> }</p><p> /**<br /> * 有編碼方式的檔案建立<br /> * @param filePathAndName 文字檔完整絕對路徑及檔案名稱<br /> * @param fileContent 文字檔內容<br /> * @param encoding 編碼方式 例如 GBK 或者 UTF-8<br /> * @return<br /> */<br /> public void createFile(String filePathAndName, String fileContent, String encoding) {</p><p> try {<br /> String filePath = filePathAndName;<br /> filePath = filePath.toString();<br /> File myFilePath = new File(filePath);<br /> if (!myFilePath.exists()) {<br /> myFilePath.createNewFile();<br /> }<br /> PrintWriter myFile = new PrintWriter(myFilePath,encoding);<br /> String strContent = fileContent;<br /> myFile.println(strContent);<br /> myFile.close();<br /> }<br /> catch (Exception e) {<br /> message = "建立檔案操作出錯";<br /> }<br /> } </p><p> /**<br /> * 刪除檔案<br /> * @param filePathAndName 文字檔完整絕對路徑及檔案名稱<br /> * @return Boolean 成功刪除返回true遭遇異常返回false<br /> */<br /> public boolean delFile(String filePathAndName) {<br /> boolean bea = false;<br /> try {<br /> String filePath = filePathAndName;<br /> File myDelFile = new File(filePath);<br /> if(myDelFile.exists()){<br /> myDelFile.delete();<br /> bea = true;<br /> }else{<br /> bea = false;<br /> message = (filePathAndName+"刪除檔案操作出錯");<br /> }<br /> }<br /> catch (Exception e) {<br /> message = e.toString();<br /> }<br /> return bea;<br /> }</p><p> /**<br /> * 刪除檔案夾<br /> * @param folderPath 檔案夾完整絕對路徑<br /> * @return<br /> */<br /> public void delFolder(String folderPath) {<br /> try {<br /> delAllFile(folderPath); //刪除完裡面所有內容<br /> String filePath = folderPath;<br /> filePath = filePath.toString();<br /> java.io.File myFilePath = new java.io.File(filePath);<br /> myFilePath.delete(); //刪除空檔案夾<br /> }<br /> catch (Exception e) {<br /> message = ("刪除檔案夾操作出錯");<br /> }<br /> }</p><p> /**<br /> * 刪除指定檔案夾下所有檔案<br /> * @param path 檔案夾完整絕對路徑<br /> * @return<br /> * @return<br /> */<br /> public boolean delAllFile(String path) {<br /> boolean bea = false;<br /> File file = new File(path);<br /> if (!file.exists()) {<br /> return bea;<br /> }<br /> if (!file.isDirectory()) {<br /> return bea;<br /> }<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 /> bea = true;<br /> }<br /> }<br /> return bea;<br /> }</p><p> /**<br /> * 複製單個檔案<br /> * @param oldPathFile 準備複製的檔案源<br /> * @param newPathFile 拷貝到新絕對路徑帶檔案名稱<br /> * @return<br /> */<br /> public void copyFile(String oldPathFile, String newPathFile) {<br /> try {<br /> int bytesum = 0;<br /> int byteread = 0;<br /> File oldfile = new File(oldPathFile);<br /> if (oldfile.exists()) { //檔案存在時<br /> InputStream inStream = new FileInputStream(oldPathFile); //讀入原檔案<br /> FileOutputStream fs = new FileOutputStream(newPathFile);<br /> byte[] buffer = new byte[1444];<br /> while((byteread = inStream.read(buffer)) != -1){<br /> bytesum += byteread; //位元組數 檔案大小<br /> System.out.println(bytesum);<br /> fs.write(buffer, 0, byteread);<br /> }<br /> inStream.close();<br /> }<br /> }catch (Exception e) {<br /> message = ("複製單個檔案操作出錯");<br /> }<br /> }</p><p> /**<br /> * 複製整個檔案夾的內容<br /> * @param oldPath 準備拷貝的目錄<br /> * @param newPath 指定絕對路徑的新目錄<br /> * @return<br /> */<br /> public void copyFolder(String oldPath, String newPath) {<br /> try {<br /> new File(newPath).mkdirs(); //如果檔案夾不存在 則建立新檔案夾<br /> File a=new File(oldPath);<br /> String[] file=a.list();<br /> File temp=null;<br /> for (int i = 0; i < file.length; i++) {<br /> if(oldPath.endsWith(File.separator)){<br /> temp=new File(oldPath+file[i]);<br /> }else{<br /> temp=new File(oldPath+File.separator+file[i]);<br /> }<br /> if(temp.isFile()){<br /> FileInputStream input = new FileInputStream(temp);<br /> FileOutputStream output = new FileOutputStream(newPath + "/" +<br /> (temp.getName()).toString());<br /> byte[] b = new byte[1024 * 5];<br /> int len;<br /> while ((len = input.read(b)) != -1) {<br /> output.write(b, 0, len);<br /> }<br /> output.flush();<br /> output.close();<br /> input.close();<br /> }<br /> if(temp.isDirectory()){//如果是子檔案夾<br /> copyFolder(oldPath+"/"+file[i],newPath+"/"+file[i]);<br /> }<br /> }<br /> }catch (Exception e) {<br /> message = "複製整個檔案夾內容操作出錯";<br /> }<br /> }</p><p> /**<br /> * 移動檔案<br /> * @param oldPath<br /> * @param newPath<br /> * @return<br /> */<br /> public void moveFile(String oldPath, String newPath) {<br /> copyFile(oldPath, newPath);<br /> delFile(oldPath);<br /> }</p><p> /**<br /> * 移動目錄<br /> * @param oldPath<br /> * @param newPath<br /> * @return<br /> */<br /> public void moveFolder(String oldPath, String newPath) {<br /> copyFolder(oldPath, newPath);<br /> delFolder(oldPath);<br /> }<br /> public String getMessage(){<br /> return this.message;<br /> }<br />}</p><p>