1. Write the content into the txt document
Copy the code code as follows:
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 = "Write the content into the txt file!" + line;
out.write(smsContent.toString().getBytes("GBK"));
out.close();
} catch (Exception ex) {
log.error("Failed to write the results to the file!", ex);
}
}
2. Read the contents of the file
Copy the code code as follows:
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("Failed to read data", ex);
} finally {
}
}
3. Clear the contents of the txt file
Copy the code code as follows:
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('Cleared email log successfully!');";
} catch (Exception ex) {
script = "alert('Clearing the contents of the file failed because no email log file was sent!');";
}
}