將內(nèi)容寫到txt文檔里面并讀取及刪除的方法
更新時(shí)間:2014年01月07日 16:02:30 作者:
本文有個(gè)不錯的示例,主要講解如何將內(nèi)容寫到txt文檔里面、讀取文件里面的內(nèi)容以及清除txt文件里面的內(nèi)容
1、將內(nèi)容寫到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 = "將內(nèi)容寫到txt文件里面!" + line;
out.write(smsContent.toString().getBytes("GBK"));
out.close();
} catch (Exception ex) {
log.error("將結(jié)果寫入文件失?。?, ex);
}
}
2、讀取文件里面的內(nèi)容
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("讀取數(shù)據(jù)失敗", ex);
} finally {
}
}
3、清除txt文件里面的內(nèi)容
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('清空發(fā)送郵件日志成功!');";
} catch (Exception ex) {
script = "alert('清空文件的內(nèi)容失敗,因?yàn)闆]有發(fā)送郵件日志文件!');";
}
}
復(fù)制代碼 代碼如下:
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 = "將內(nèi)容寫到txt文件里面!" + line;
out.write(smsContent.toString().getBytes("GBK"));
out.close();
} catch (Exception ex) {
log.error("將結(jié)果寫入文件失?。?, ex);
}
}
2、讀取文件里面的內(nèi)容
復(fù)制代碼 代碼如下:
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("讀取數(shù)據(jù)失敗", ex);
} finally {
}
}
3、清除txt文件里面的內(nèi)容
復(fù)制代碼 代碼如下:
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('清空發(fā)送郵件日志成功!');";
} catch (Exception ex) {
script = "alert('清空文件的內(nèi)容失敗,因?yàn)闆]有發(fā)送郵件日志文件!');";
}
}
相關(guān)文章
java如何實(shí)現(xiàn)socket連接方法封裝
這篇文章主要介紹了java實(shí)現(xiàn)socket連接方法封裝教程,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
SpringBoot項(xiàng)目中使用Groovy腳本的示例代碼
本文主要介紹了SpringBoot項(xiàng)目中使用Groovy腳本的示例代碼,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
淺談Mybatis中resultType為hashmap的情況
這篇文章主要介紹了淺談Mybatis中resultType為hashmap的情況,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12
Javabean基于xstream包實(shí)現(xiàn)轉(zhuǎn)XML文檔的方法
這篇文章主要介紹了Javabean基于xstream包實(shí)現(xiàn)轉(zhuǎn)XML文檔的方法,結(jié)合具體實(shí)例形式分析了xstream包用于轉(zhuǎn)換xml文件的具體使用技巧,需要的朋友可以參考下2017-05-05
springboot cloud使用eureka整合分布式事務(wù)組件Seata 的方法
這篇文章主要介紹了springboot cloud使用eureka整合分布式事務(wù)組件Seata 的方法 ,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-05-05
SpringBoot如何實(shí)現(xiàn)word文檔轉(zhuǎn)pdf
這篇文章主要介紹了SpringBoot如何實(shí)現(xiàn)word文檔轉(zhuǎn)pdf,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07

