ファイル リーダー関数を最適化したいのですが、try ループの外で null を宣言するのがベスト プラクティスかどうかわかりません。また、Stringbuffer に文字をループして追加することは悪い習慣と見なされますか? ここでは例外処理を使いたいのですが、別の仕組みを使った方が良いのではないでしょうか? どんなアドバイスでも大歓迎です、ありがとう。
public String readFile(){
File f = null;
FileReader fr = null;
StringBuffer content = null;
try{
f = new File("c:/test.txt");
fr = new FileReader(f);
int c;
while((c = fr.read()) != -1){
if(content == null){
content = new StringBuffer();
}
content.append((char)c);
}
fr.close();
}
catch (Exception e) {
throw new RuntimeException("An error occured reading your file");
}
return content.toString();
}
}