將內(nèi)容寫(xiě)到txt文檔里面并讀取及刪除的方法
更新時(shí)間:2014年01月07日 16:02:30 作者:
本文有個(gè)不錯(cuò)的示例,主要講解如何將內(nèi)容寫(xiě)到txt文檔里面、讀取文件里面的內(nèi)容以及清除txt文件里面的內(nèi)容
1、將內(nèi)容寫(xiě)到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)容寫(xiě)到txt文件里面!" + line;
out.write(smsContent.toString().getBytes("GBK"));
out.close();
} catch (Exception ex) {
log.error("將結(jié)果寫(xiě)入文件失??!", 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)闆](méi)有發(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)容寫(xiě)到txt文件里面!" + line;
out.write(smsContent.toString().getBytes("GBK"));
out.close();
} catch (Exception ex) {
log.error("將結(jié)果寫(xiě)入文件失??!", 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)闆](méi)有發(fā)送郵件日志文件!');";
}
}
相關(guān)文章
JVM系列之:再談java中的safepoint說(shuō)明
這篇文章主要介紹了JVM系列之:再談java中的safepoint說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09
java八大經(jīng)典書(shū)籍 你看過(guò)幾本?
java八大經(jīng)典書(shū)籍,你看過(guò)幾本?本文為大家分享了java學(xué)習(xí)書(shū)單,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
SpringCloud Zuul實(shí)現(xiàn)動(dòng)態(tài)路由
這篇文章主要介紹了SpringCloud Zuul實(shí)現(xiàn)動(dòng)態(tài)路由,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01
Java讀取網(wǎng)頁(yè)內(nèi)容并下載圖片的實(shí)例
這篇文章主要介紹了Java讀取網(wǎng)頁(yè)內(nèi)容并下載圖片的實(shí)例的相關(guān)資料,希望通過(guò)本文能幫助到大家,讓大家實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下2017-09-09
JavaSE中compare、compareTo的區(qū)別
本文主要介紹了JavaSE中compare、compareTo的區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05
完美解決MybatisPlus插件分頁(yè)查詢(xún)不起作用總是查詢(xún)?nèi)繑?shù)據(jù)問(wèn)題
這篇文章主要介紹了解決MybatisPlus插件分頁(yè)查詢(xún)不起作用總是查詢(xún)?nèi)繑?shù)據(jù)問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08
最全面的JVM優(yōu)化經(jīng)驗(yàn)總結(jié)
這篇文章主要介紹了最全面的JVM優(yōu)化經(jīng)驗(yàn)總結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,,需要的朋友可以參考下2019-06-06

