[英]Write to text file in Java WebService
對於學校,我必須將 Web 服務作為練習的一部分。 在這個程序中,我必須使用將用作數據庫的.txt 文件。 當我使用 BufferedWriter 時,它表示信息已成功寫入文件。 但文件仍然沒有改變。 從文件中讀取進展順利。 提前致謝!
編碼:
@WebService
public class Vak {
@WebMethod
public boolean addLesson(String lessonname, double mark){
if(!lessonname.equals("") || !(mark == 0.00)){
try{
FileInputStream fis = new FileInputStream("C:/marks.txt");
DataInputStream in = new DataInputStream(fis);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null){
String [] splitted = strLine.split(" ");
if(splitted[0].equalsIgnoreCase(lessonname)){
System.out.println("Lesson already exists");
return false;
}
}
BufferedWriter out = new BufferedWriter(new FileWriter("C:/marks.txt", true));
out.newLine();
out.write(lessonname + " " + mark);
return true;
}
catch(Exception e){
System.out.println("Exception " + e);
return false;
}
}
return false;
}
}
添加
out.close();
在您return true;
從文檔中, close
方法執行以下操作:
關閉 stream,先沖洗它。 一旦 stream 被關閉,進一步的 write() 或 flush() 調用將導致拋出 IOException。 關閉之前關閉的 stream 無效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.