간단한 파일 읽기 및 쓰기 기능이 있습니다.BufferedReader에서 close() 예외를 catch해야하지만 PrintWriter에서 catch하지 않아야하는 이유는 무엇입니까?
private void WriteToFile(String filename, String val) {
PrintWriter outStream = null;
FileOutputStream fos = null;
try {
fos = new FileOutputStream(filename);
outStream = new PrintWriter(new OutputStreamWriter(fos));
outStream.print(val);
outStream.close();
} catch (Exception e) {
if (outStream != null) {
outStream.close();
}
}
}
private String ReadFile(String filename) {
String output = "";
FileReader fr = null;
BufferedReader br = null;
try {
fr = new FileReader(filename);
br = new BufferedReader(fr);
output = br.readLine();
br.close();
} catch (Exception e) {
if (br != null) {
br.close();
}
}
return output;
}
언제 얻을 건물 :
unreported exception java.io.IOException; must be caught or declared to be thrown
br.close();
^
이유는 br.close을 잡을 필요합니까하지만 WriteToFile의 가까이에 대해 불평하지 않습니다()?
에()'이동'br.close을 시도해보세요'고대'PrintWriter' 설계되어 단지 방법은 그 –
을 차단 finally'. 그 메소드 중 어느 것도'IOException'을 던지지 않습니다. 대신에'FileWriter'를 사용하십시오. – BalusC
나는 읽기에 더 염려합니다. close 메소드가 예외를 throw하지 않는 '동등한'독자가 있습니까? – Grazfather