/**
* 读取文件
*/
public static String readFile(String pathname) {
StringBuffer str = new StringBuffer();
//防止文件建立或读取失败,用catch捕捉错误并打印
try (FileReader reader = new FileReader(pathname);
BufferedReader br = new BufferedReader(reader)
) {
String line;//逐行读取
while ((line = br.readLine()) != null) {
str.append(line);
str.append("\r\n");
}
} catch (IOException e) {
e.printStackTrace();
}
return str.toString();
}
/**
* 写入文件,如果文件存在,追加写入
*/
public static void writeFile(String pathname, String content) {
try {
File writeName = new File(pathname);
try (FileWriter writer = new FileWriter(writeName, true);
BufferedWriter out = new BufferedWriter(writer)
) {
out.write(content);
out.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
}