寫爬蟲所用到的工具類---(3)[檔案]
阿新 • • 發佈:2018-12-25
package Tool;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
/**
* this is a class that can operation file in the local,
* Copyright (C) 2016-2020 All Rights Reserved.
*
* @author
* @version 0.0.1
* @date 2016/11/3
*/
public class LocalFileUntil {
private LocalFileUntil() {
}
/**
* this is a method that can create a file ,you should offer a dirPath that
* you need create a file in where.
*
* @param dirPath 檔案的目錄路徑
* @param fileName 建立的檔案的名字
*/
public static void creatFile(String dirPath, String fileName) {
File fileDir = new File(dirPath);
if (!fileDir.exists()) {
fileDir.mkdirs();
}
String filePath = dirPath + File.separator + fileName;
File file = new File(filePath);
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 以追加的形式向檔案中寫入內容
*
* @param writeFilePath 要寫入內容的檔案的路徑
* @param content 要寫入的內容
* @return 成功寫入
*/
public static synchronized boolean writeFlieOneLine(String writeFilePath, String content) {
try {
FileWriter fileWriter = new FileWriter(writeFilePath, true);
fileWriter.write(content.toString() + System.getProperty("line.separator"));
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
/**
* 以多行的形式寫入檔案內容
*
* @param writeDirFilePath 寫入的檔案的目錄路徑
* @param fileName 寫入檔案的名字
* @param contentList 需要一個List的字串
*/
public static synchronized void writeFileByMultiple(String writeDirFilePath, String fileName, List<String> contentList) {
//建立檔案
creatFile(writeDirFilePath, fileName);
String filePath = writeDirFilePath + File.separator + fileName;
File file = new File(filePath);
try {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file, true));
for (int i = 0; i < contentList.size(); i++) {
bufferedWriter.write(contentList.get(i));
bufferedWriter.newLine();
}
bufferedWriter.flush();
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 按行讀取檔案的內容,需要提供要讀取檔案的路徑以及編碼格式
*
* @param filePath 要讀取檔案的路徑
* @param encoding 編碼格式
* @return 返回的是一個List<String>型別的字元陣列
*/
public static List<String> readFileByLine(String filePath, String encoding) {
List<String> sb = new ArrayList<String>();
try {
File file = new File(filePath);
if (file.exists() && file.isFile()) {
InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(file), encoding);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String txtLine = bufferedReader.readLine();
while (txtLine != null) {
sb.add(txtLine);
txtLine = bufferedReader.readLine();
}
bufferedReader.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return sb;
}
/**
* 讀取檔案的所有內容(速度比readFileToString慢)
*
* @param filePath 所要讀取檔案的路徑
* @param encoding 檔案的編碼
* @return 返回一個String型別的字串,儲存檔案的所有內容
*/
public static String readFileByAllContent(String filePath, String encoding) {
StringBuffer sb = new StringBuffer();
try {
File file = new File(filePath);
if (file.exists() && file.isFile()) {
InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(file), encoding);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String txt = bufferedReader.readLine();
String separator = System.getProperty("line.separator");
while (txt != null) {
sb.append(txt);
sb.append(separator);
txt = bufferedReader.readLine();
}
bufferedReader.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString();
}
/**
* 以字串的形式讀取檔案的全部內容
*
* @param filePath 要讀取檔案的路徑
* @param encoding 該檔案所使用的編碼
* @return 一個String型別的變數,儲存著檔案中的所有的內容
*/
public static String readFileToString(String filePath, String encoding) {
File file = new File(filePath);
Long fileLength = file.length();
byte[] fileContent = new byte[fileLength.intValue()];
try {
FileInputStream in = new FileInputStream(file);
in.read(fileContent);
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
try {
return new String(fileContent, encoding);
} catch (UnsupportedEncodingException e) {
System.err.println("The OS don't support the " + encoding);
e.printStackTrace();
return null;
}
}
}