下面 These 幾個方法是依日常常用需求組裝出來的檔案操作工具類。平時我更偏好 Jodd.io 提供的那些工具類,方法寫得簡單易懂;其中 JavaUtil 類的 append、read 等方法封裝良好、也更貼近物件導向,我這裡的不少實作其實就是從中抽取出來改寫的。
/**
* 獲取路徑文件夾下的所有文件
*/
public static File[] getKeywordFiles(String path) {
File dir = new File(path);
if (!dir.exists())
return null;
File[] fs = dir.listFiles();
return fs;
}
/**
* 刪除文件夾 param folderPath 文件夾完整絕對路徑
*/
private static void delFolder(String folderPath) {
try {
delAllFile(folderPath); // 刪除完裡面所有內容
String filePath = folderPath;
filePath = filePath.toString();
java.io.File myFilePath = new java.io.File(filePath);
myFilePath.delete(); // 刪除空文件夾
} catch (Exception e) {
log.error(e);
}
}
/**
* 讀取一個文件
*/
public static List<String> readFile(String filePathAndName)
throws IOException {
FileInputStream fis = new FileInputStream(filePathAndName);
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
BufferedReader br = new BufferedReader(isr);
LineNumberReader lnr = new LineNumberReader(br);
List<String> returnValue = new ArrayList<String>();
int cnt = 0;
while (true) {
cnt++;
String tempStr = lnr.readLine();
if (tempStr == null)
break;
if (tempStr.length() < 2)
continue;
returnValue.add(tempStr);
}
lnr.close();
br.close();
isr.close();
fis.close();
return returnValue;
}
/**
* 讀取一個文件,並排重後返回
*/
public static List<String> readFileNoDup(String filePathAndName)
throws IOException {
FileInputStream fis = new FileInputStream(filePathAndName);
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
BufferedReader br = new BufferedReader(isr);
LineNumberReader lnr = new LineNumberReader(br);
Set<String> set = new HashSet<String>();
while (true) {
String tempStr = lnr.readLine();
if (tempStr == null)
break;
if (tempStr.length() < 2)
continue;
set.add(tempStr.trim());
}
lnr.close();
br.close();
isr.close();
fis.close();
List<String> returnValue = new ArrayList<String>(set.size());
returnValue.addAll(set);
return returnValue;
}
/**
* 添加內容到指定文件。文件不存在則創建;已存在時 flag 為 true 追加,否則覆蓋
*/
public static void writeFile(String filePathAndName, String fileContent,
boolean flag) throws IOException {
if (null == fileContent || fileContent.length() < 1)
return;
File file = new File(filePathAndName);
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream fos = new FileOutputStream(filePathAndName, flag);
OutputStreamWriter osw = new OutputStreamWriter(fos, "utf-8");
osw.write(fileContent + "\r\n");
osw.flush();
osw.close();
}
/**
* 以列表形式寫入,flag 為 true 追加,否則覆蓋
*/
public static void writeFile(String filePathAndName,
List<String> fileContent, boolean flag) throws IOException {
File file = new File(filePathAndName);
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream fos = new FileOutputStream(filePathAndName, flag);
OutputStreamWriter osw = new OutputStreamWriter(fos, "utf-8");
for (String temp : fileContent)
osw.write(temp + "\r\n");
osw.flush();
osw.close();
}
/**
* 指定目錄與檔名寫入,目錄不存在時自動建立
*/
public static void writeFile(String filePath, String filename,
List<String> fileContent, boolean flag) throws IOException {
File file = new File(filePath);
if(!file.exists()){
boolean tempFlag = file.mkdirs();
if(!tempFlag){
log.error("文件夾" + filePath + "創建失敗");
return;
}
}
file = new File(filePath, filename);
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream fos = new FileOutputStream(filePath + filename, flag);
OutputStreamWriter osw = new OutputStreamWriter(fos, "utf-8");
for (String temp : fileContent)
osw.write(temp + "\r\n");
osw.flush();
osw.close();
}
這批方法共有三個共同設計:固定以 UTF-8 編碼、透過 flag 決定追加或覆蓋、以及讀取時主動過濾掉長度小於 2 的短行。三個 writeFile 重載分別對應「單一字串」、「字串列表」、「目錄加檔名」三種常見需求。
若要在正式專案中使用,建議再補上 try-with-resources,讓串流一定被關閉;另外 readFile 過濾短行的規則屬於業務假設,重用前先確認是否符合你的資料格式。