Java操作文件的工具類:FileUtil
package com.wiseweb.util;import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; public class FileUtil { // /** // * 獲取獲取系統根目錄下xml文件的路徑的方法; // */ // @SuppressWarnings("unused") // private String projectFilePath = new File("").getAbsolutePath() // + "\\config.xml"; // // /** // * 獲取Web項目發布后(源碼中src下)classes目錄下某個xml路徑的方法; // */ // @SuppressWarnings("unused") // private String srcFilePath = getClass().getClassLoader().getResource( // "config.xml").getPath(); File file = null; boolean flag = false; /** * 寫入txt文件,可以在原文件內容的基礎上追加內容(并判斷目錄是否存在,不存在則生成目錄) * * @param value * 寫入文件內容 * @param fileCatage * 文件父目錄; * @param fileName * 文件名字; * @param code * 文件的編碼; * @throws IOException */ public void WriteFile(String value, String fileCatage, String fileName, String code) { File file = null; try { file = new File(fileCatage); if (!file.isDirectory()) file.mkdir(); else { file = new File(fileCatage + fileName); if (!file.exists()) file.createNewFile(); FileOutputStream out = new FileOutputStream(file, true); out.write(value.getBytes(code)); out.close(); } } catch (IOException e) { e.printStackTrace(); } } /*** * 覆蓋原來的內容; * * @param filePath * 文件的路徑 * @param content * 保存的內容; * @return */ public boolean saveFile(String filePath, String content) { boolean successful = true; FileOutputStream fout = null; try { fout = new FileOutputStream(new File(filePath), false); fout.write(content.getBytes()); } catch (FileNotFoundException e1) { successful = false; } catch (IOException e) { successful = false; } finally { if (fout != null) { try { fout.close(); } catch (IOException e) { } } } return successful; } /** * 刪除文件的綜合操作( 根據路徑刪除指定的目錄或文件,無論存在與否) * * @param sPath * 要刪除的目錄或文件 *@return 刪除成功返回 true,否則返回 false。 */ public boolean DeleteFolder(String sPath) { flag = false; file = new File(sPath); // 判斷目錄或文件是否存在 if (!file.exists()) { // 不存在返回 false return flag; } else { // 判斷是否為文件 if (file.isFile()) { // 為文件時調用刪除文件方法 return deleteFile(sPath); } else { // 為目錄時調用刪除目錄方法 return deleteDirectory(sPath); } } } /** * 刪除單個文件 * * @param sPath * 被刪除文件的文件名 * @return 單個文件刪除成功返回true,否則返回false */ public boolean deleteFile(String sPath) { flag = false; file = new File(sPath); // 路徑為文件且不為空則進行刪除 if (file.isFile() && file.exists()) { file.delete(); flag = true; } return flag; } /** * 刪除目錄(文件夾)以及目錄下的文件 * * @param sPath * 被刪除目錄的文件路徑 * @return 目錄刪除成功返回true,否則返回false */ public boolean deleteDirectory(String sPath) { // 如果sPath不以文件分隔符結尾,自動添加文件分隔符 if (!sPath.endsWith(File.separator)) { sPath = sPath + File.separator; } File dirFile = new File(sPath); // 如果dir對應的文件不存在,或者不是一個目錄,則退出 if (!dirFile.exists() || !dirFile.isDirectory()) { return false; } flag = true; // 刪除文件夾下的所有文件(包括子目錄) File[] files = dirFile.listFiles(); for (int i = 0; i < files.length; i++) { // 刪除子文件 if (files[i].isFile()) { flag = deleteFile(files[i].getAbsolutePath()); if (!flag) break; } // 刪除子目錄 else { flag = deleteDirectory(files[i].getAbsolutePath()); if (!flag) break; } } if (!flag) return false; // 刪除當前目錄 if (dirFile.delete()) { return true; } else { return false; } } /** * 按字節【讀】取文件的內容; * * @param Offset * 讀取內容的開始出 * @param length * 內容的長度; * @param filePath * 文件的路徑; * @param code * 編碼; * @return 返回相應的內容; * @throws Exception */ public String readFileByByte(int Offset, int length, String filePath, String code) { File file = new File(filePath); FileInputStream fis = null; try { fis = new FileInputStream(file); } catch (FileNotFoundException e) { e.printStackTrace(); return null; } try { fis.skip(Offset); } catch (IOException e) { e.printStackTrace(); return null; } byte[] bytes = new byte[length]; try { fis.read(bytes); } catch (IOException e) { e.printStackTrace(); return null; } try { fis.close(); } catch (IOException e) { e.printStackTrace(); return null; } try { return new String(bytes, code); } catch (UnsupportedEncodingException e) { e.printStackTrace(); return null; } } /** * 將流中的文本讀入一個 BufferedReader 中 * * @param filePath * 文件路徑 * @param code * 編碼格式 * @return * @throws IOException */ public BufferedReader readToBufferedReader(String filePath, String code) throws IOException { BufferedReader bufferedReader = null; File file = new File(filePath); if (file.isFile() && file.exists()) { // 判斷文件是否存在 InputStreamReader read = new InputStreamReader(new FileInputStream( file), code);// 考慮到編碼格式 bufferedReader = new BufferedReader(read); } return bufferedReader; } /** * 將流中的文本讀入一個 StringBuffer 中 * * @param filePath * 文件路徑 * @throws IOException */ public StringBuffer readToBuffer(String filePath, String code) { StringBuffer buffer = new StringBuffer(); InputStream is; try { File file = new File(filePath); if (!file.exists()) return null; is = new FileInputStream(filePath); String line; // 用來保存每行讀取的內容 BufferedReader reader = new BufferedReader(new InputStreamReader( new FileInputStream(file), code)); line = reader.readLine(); // 讀取第一行 while (line != null) { // 如果 line 為空說明讀完了 buffer.append(line); // 將讀到的內容添加到 buffer 中 // buffer.append("\n"); // 添加換行符 line = reader.readLine(); // 讀取下一行 } reader.close(); is.close(); } catch (Exception e) { e.printStackTrace(); } return buffer; } public String loadFile(String filePath, String charset) { FileInputStream fin = null; StringBuffer sb = new StringBuffer(); try { fin = new FileInputStream(new File(filePath)); byte[] buffer = new byte[Integer.MAX_VALUE]; int start = -1; while ((start = fin.read(buffer)) != -1) { sb.append(new String(buffer, 0, start, charset)); } } catch (Exception e) { e.printStackTrace(); } finally { if (fin != null) { try { fin.close(); } catch (IOException e) { } } } return sb.toString(); } /** * 獲取某個目錄下所有文件或者獲取某個文件的大小; 單位:MB * * @param file * @return */ public static double getDirSize(File file) { // 判斷文件是否存在 if (file.exists()) { // 如果是目錄則遞歸計算其內容的總大小 if (file.isDirectory()) { File[] children = file.listFiles(); double size = 0; for (File f : children) size += getDirSize(f); return size; } else {// 如果是文件則直接返回其大小,以“兆”為單位 double size = (double) file.length() / 1024 / 1024; return size; } } else { System.out.println("獲取文件大小錯誤!!文件或者文件夾不存在,請檢查路徑是否正確!"); return 0.0; } } /** * 獲取某個目錄下所有的文件的全路徑和文件名的集合; * * @return */ public List<List<String>> getAllFile(String mulu) { File file = new File(mulu); File[] files = file.listFiles(); List<List<String>> ret = new ArrayList<List<String>>(); List<String> allFilePath = new ArrayList<String>(); List<String> allFileName = new ArrayList<String>(); for (int i = 0; i < files.length; i++) { if (files[i].isDirectory()) { allFilePath.add(files[i].toString()); allFileName.add(files[i].getName()); } } ret.add(allFilePath); ret.add(allFileName); return ret; } } </pre><br />
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!