例外と例外に関するコメントをテキストファイルに保存するコードを書いています。また、StoreErrorsクラスの新しいインスタンスを呼び出すたびに、ファイルの最後にデータを書き込む代わりに、エラーファイルが書き換えられるという問題が発生しています。
public StoreErrors(Exception e){
//increment the error number as the number of times StoreErrors
//was intialized
errorNum +=1;
try{
FileOutputStream toWriter;
if(!errReport.exists()){
boolean isCreated =errReport.createNewFile();
if(isCreated){
System.out.println("No Error Report was found a new one "
+ "has been created");
}
/*if the file is already present set append to file to true on
* FileOutputStream
*/
toWriter=new FileOutputStream(errReport, true);
}else{
toWriter=new FileOutputStream(errReport);
}
//OutputStreamWriter allows toLog to be writen in UTF8
//BufferedWriter Takes characters from the OutputStreamWriter
/*Which Writes to the file using the File errReport using
* FileOutputStream toWriter
*/
toLog=new BufferedWriter(new OutputStreamWriter(
toWriter, "UTF8"));
/*Creates and exception object which can be used to get information
* about the error that occured
*/
storeException=e;
}catch (UnsupportedEncodingException encEx){
encEx.printStackTrace();
}catch(IOException ioEx){
ioEx.printStackTrace();
}catch (Exception ex){
ex.printStackTrace();
}
}
これは、新しいエラーが格納されるたびに呼び出されるコンストラクターです。BufferedReader(OutputStreamWriter(FileOutputStream(File、append boolean)、Encoding))の組み合わせが正しいかどうかを教えてください。errorNumとerrReportはどちらも静的です。ファイル宣言は以下のとおりです。
プライベート静的ファイルerrReport=new File( "err_Report.txt");
また、実際にライターを使用してファイルに書き込むときに、appendの代わりにtoLog.write( "string" + "\ r \ n")を使用しました。クラスを呼び出すたびに同じファイルの末尾が追加されるようにするにはどうすればよいのでしょうか。また、ファイルを現在のコードで上書きするのはなぜですか。