2012-10-19 3 views
1

에 자바에서 파일을 열면 아래의 코드 스 니펫은이 오류가 발생합니다은 OS/X

File dir = new File(path); 
String[] chld = dir.list(); 
if(chld == null){ 
    System.out.println("Specified directory does not exist or is not a directory."); 
    System.exit(0); 
} else { 
    for(int i = 0; i < chld.length; i++){ 
     String fileName = chld[i]; 
     System.out.println(fileName); 
    } 
} 
:이 코드로 나열된 파일을 볼 수 있기 때문에

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
Unhandled exception type FileNotFoundException 
String path = "/Users/jfaig/Documents/workspace/my_array/"; 
BufferedReader in = new BufferedReader(new FileReader(path + "Matrix.txt")); 

경로는 유효합니다

Java에서 OS/X 경로에 대한 많은 기사를 검토했지만 아무도 내 문제를 해결하지 못했습니다. Windows PC에서이 문제를 OS/X 및/또는 Eclipse 설치와 관련이 있는지 확인하려고합니다.

답변

0

java.lang.Error: Unresolved compilation problem:실제 오류가 javac의 출력시에 나열되어 있음을 의미하지만, 당신을 위해 여기를 반복합니다. Unhandled exception type FileNotFoundException - 자바의 예외를 명시 적으로 포착하거나 다시 throw해야합니다.

3

파일에 대해 불평하지 않고이지만 파일을 찾을 수없는 경우 처리하도록 요청합니다. 당신이 예외를 처리하려면

public static void main(String[] args) throws FileNotFoundExcetion { 

를, 또는 :이 시나리오에 대해 수행 어떤 취급, main 방법에 대한 throws FileNotFoundException 예와 방법 서명을 업데이트하지 않으려면

, 당신은 다음과 같이 쓸 수 있습니다

try{ 
    File dir = new File(path); 
    String[] chld = dir.list(); 
    if(chld == null){ 
    System.out.println("Specified directory does not exist or is not a directory."); 
    System.exit(0); 
    } else { 
    for(int i = 0; i < chld.length; i++){ 
     String fileName = chld[i]; 
     System.out.println(fileName); 
    } 
    }    
    }catch(FileNotFoundException fnfe){ 
    //log error 
    /System.out.println("File not found"); 
}   
0

하자 자바 경로와 파일 구분을하고 두 개의 인수 파일 생성자를 사용 : 아래와 같은 try{}catch(FileNotFoundException fnfe){} 블록에 위의 코드를 포장.

File path = new File("/Users/jfaig/Documents/workspace/my_array"); 
File file = new File(path, "Matrix.txt"); 
System.out.println("file exists=" + file.exists()); // debug 
BufferedReader in = new BufferedReader(new FileReader(file)); 

앞서 언급했듯이 메소드를 catch하거나 IOException을 throw해야합니다.