2014-06-12 3 views
0

자바에 텍스트 파일을로드하려고하는데 코드에서 파일을 찾는 데 문제가 있습니다. 내 파일의 이름은 'test.txt'이며 src/test.txt, src/fileIOTest/test.txt 및 리소스 파일에 저장되지만 파일로드 방법에 관계없이 프로그램에서 그것을 찾은 것 같습니다. 그것은 패키지 탐색기에 나타나지만 예외가 발견되지 않는 파일을 던집니다.스캐너 문제 찾기 파일

package fileIOTest; 

//This project tests out the scanner system, reading a text file into an array. 
import java.io.IOException; 
import java.util.Scanner; 
import java.io.*; 

public class Test { 

public void init(){ 


    Scanner sc = null; 
    try { 
     sc = new Scanner(new FileReader("/test.txt")); 
    } catch(IOException e){ 
     e.printStackTrace();// print the error 
    } 

    int[] testArray = new int[10]; 

    for(int i = 0; i < 10; i++){ 
     testArray[i] = sc.nextInt(); 
     System.out.println(testArray[i]); 
     } 

    } 


public static void main(String[] args){ 
    Test fileLoader = new Test(); 
    fileLoader.init(); 

    } 
} 

편집 : 여기 내 파일 시스템의 Here's my file system

+0

부터 대시를 제거/test.txt ")) –

+0

그 중 하나도 행운이 없습니다. – fostythesnowman

+1

그는 슬래시 (slash)를 의미하며, 일반적으로 이런 종류의 작업을 위해 getResourceAsStream을 사용해야합니다. – chrylis

답변

0

좋아, 시도 : 슬래시가 작동 할 수 제거해보십시오 :

File directory = new File("src/test.txt"); 
Path file = Paths.get(directory.getAbsolutePath()); 
Scanner sc = new Scanner(file); 

또는 "(

Scanner sc = newScanner(new FileReader("test.txt")); 
+0

예! 그게 효과가 있었어! 고마워요! – fostythesnowman