程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 將內容寫到txt文檔外面並讀取及刪除的辦法

將內容寫到txt文檔外面並讀取及刪除的辦法

編輯:關於JAVA

將內容寫到txt文檔外面並讀取及刪除的辦法。本站提示廣大學習愛好者:(將內容寫到txt文檔外面並讀取及刪除的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是將內容寫到txt文檔外面並讀取及刪除的辦法正文


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('清空文件的內容掉敗,由於沒有發送郵件日記文件!');";
}
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved