2012-04-14 7 views
0

나는 내 시간에 프로그래밍을 배우려고 노력하고 있으며, 여전히 그것에 매달려 있습니다. 나는 다음과 같은 오류가 점점 오전 : 여기java.io.IOException : 핸들이 유효하지 않습니다.

java.io.IOException: The handle is invalid

것은 내가 바이트의 수를 표시 할 때 내가, 내가 arrayListWithNumbers() 메소드를 호출 할 때마다 나는 오류를 받고 있어요 생각 내 코드

public class PrimeFinder { 
private int[] prime; 
FileInputStream input = null; 
//default contructor uses the premade file prime.txt that has the first 10000 digits of pi 
public PrimeFinder() throws IOException { 
    try{ 
     input = new FileInputStream ("primes.txt"); 
    } 
    finally { 
     if (input != null) { 
      input.close(); 
     } 
    } 
} 
//constructor with a predefined text file to use. 
public PrimeFinder(String txtFile) throws IOException { 
    try{ 
     input = new FileInputStream(txtFile); 
    } 
    finally { 
     if (input != null) { 
      input.close(); 
     } 
    } 
} 
public static void main(String[] args) throws IOException{ 

    PrimeFinder tester = new PrimeFinder(); 
    tester.arrayListWithNumbers(); 
} 
} 

입니다 기본 생성자, 그럼 그것은 완벽하게 작동하고 101281 bytes의 집계를 보여줍니다.

+2

오류가 발생한 위치를 추측 할 필요가 없습니다. 스택 추적은 발생한 지점을 정확하게 추적합니다. 코드에 대한 참조가 나타날 때까지 호출 체인을 추적하십시오. –

답변

2

실제로 사용하기 전에 생성자 finally 블록의 input 블록을 종료합니다. arrayListWithNumbers에 대한 호출 아래 또는 사용자가 main에서 호출 한 별도의 닫기 메서드와 같이 완료 될 때 생성자에서 닫는 부분을 어딘가에 이동합니다.

finallyfinalize()을 혼동하는 것으로 생각합니다.이 용도로 사용해서는 안됩니다.

관련 문제