1.將內容寫到txt文檔裡面
複製代碼代碼如下:
public static void writeFile() {
String txtFileName = "emailRecord.txt";
String directoryPath = "";
try {
directoryPath = WebplusContext.getRealPath("/apps/schoolfellow/upload/smsRecord");
File directory = new File(directoryPath);
if (!directory.exists()) {
directory.mkdirs();
}
File txtFile = new File(directoryPath, txtFileName);
FileOutputStream out = new FileOutputStream(txtFile, true);
String line = System.getProperty("line.separator");
String smsContent = "將內容寫到txt檔案裡面!" + line;
out.write(smsContent.toString().getBytes("GBK"));
out.close();
} catch (Exception ex) {
log.error("將結果寫入檔案失敗!", ex);
}
}
2、讀取文件裡面的內容
複製代碼代碼如下:
public void readerFile() {
String filePath = WebplusContext.getServletContext().getRealPath("/apps/schoolfellow/upload/emailRecord/emailRecord.txt");
FileInputStream fis = null;
try {
fis = new FileInputStream(filePath);
InputStreamReader reader = new InputStreamReader(fis, "GBK");
BufferedReader br = new BufferedReader(reader);
String info = "";
schoolfellows = new ArrayList<SchoolfellowDataViewWrap>();
while ((info = br.readLine()) != null) {
System.out.println(info);
}
br.close();
fis.close();
} catch (Exception ex) {
log.error("讀取資料失敗", ex);
} finally {
}
}
3.清除txt檔案裡面的內容
複製代碼代碼如下:
public void clearFileContent() {
String filePath = WebplusContext.getServletContext().getRealPath("/apps/schoolfellow/upload/emailRecord/emailRecord.txt");
try {
FileOutputStream out = new FileOutputStream(filePath,false);
out.write(new String("").getBytes());
out.close();
script = "alert('清空發送郵件日誌成功!');";
} catch (Exception ex) {
script = "alert('清空檔案的內容失敗,因為沒有傳送郵件日誌檔案!');";
}
}