파일에 쓰고 같은 파일에서 읽으려고합니다. 출력은 "오류 : I/O 예외"입니다. 프로그램이 IOException을 잡는다는 것을 의미합니다.Java 파일 I/O : 항상 I/O 예외가 발생하는 이유는 무엇입니까?
public class fileIO {
public static void main(String[] args) {
// TODO Auto-generated method stub
try
{
File file = new File("io.txt");
BufferedReader read = new BufferedReader(new FileReader(file));
BufferedWriter write = new BufferedWriter(new FileWriter(file));
String needs = "This is going to the file";
write.write(needs);
String stuff = read.readLine();
while(stuff != null)
{
System.out.println(stuff);
stuff = read.readLine();
}
}
catch(IOException e)
{
System.out.println("Error: I/O Exception");
}
catch(NullPointerException e)
{
System.out.println("Error: NullPointerException");
}
}
}'
당신은 어떤 예외를받을 수 있나요? 'FileNotFound'? – MadProgrammer
stacktrace를보고 질문에 추가해야합니다. catch 블록에서'e.printStackTrace()'를 호출함으로써 간단히 처리 할 수있다. – mkobit
스택 추적을 인쇄하면 무엇이 잘못되었는지 자세히 알 수 있습니다. 그냥'main'이 예외를 잡는 대신에 예외를 던지게하십시오. –