프로젝트 용 클래스를 작성 중이며 아래에 포함 된 sampleSearch.txt라는 파일을 읽어야합니다. 문제는 이클립스를 통해 실제로 파일을로드 할 수없는 것처럼 보입니다. 텍스트 파일이 나머지 코드와 동일한 디렉토리에 있어도 항상 파일을 찾을 수 없습니다.FileReader를 사용하여 파일을 찾을 수 없습니다.
import java.util.*;
import java.io.*;
public class Driver{
public static void main (String[] args){
// try block needed to read in file
try
{
//open the file "sampleSearch.txt"
FileReader fileName = new FileReader("sampleSearch.txt");
Scanner fileRead = new Scanner(fileName);
//read in the number of rows
int numRow = fileRead.nextInt();
fileRead.nextLine();
//read in the number of columns
int numCol = fileRead.nextInt();
fileRead.nextLine();
//create a 2d array to hold the characters in the file
char[][] array = new char[numRow][numCol];
//for each row in the file
for (int r = 0; r < numRow; r++)
{
//read in the row as a string
String row = fileRead.nextLine();
//parse the string into a sequence of characters
for (int c = 0; c < numCol; c++)
{
//store each character in the 2d array
array[r][c] = row.charAt(c);
}
}
//create a new instance of the WordSearch class
WordSearch ws = new WordSearch(array);
//play the game
ws.play();
}
catch (FileNotFoundException exception)
{
//error is thrown if file cannot be found. See directions or email me...
System.out.println("File Not Found gribble");
}
}
}
당신은 어떤 디렉토리에 코드가 파일을 검색하고 알 필요가
파일은 어디에 저장됩니까? 동일한 실행 컨텍스트 (즉, 일부 디렉토리) 내에서 프로그램을 실행하고 있습니까? – MadProgrammer
파일은 eclipse가 만든 동일한 디렉토리 (lab5/src/lab5)에 저장됩니다. – KevinRandall
'src' 디렉토리의 내용이 프로그램의 실행 위치를 구성하지 않으며 대부분의 경우 Jar 파일 'src' 디렉토리 위의'lab5' 디렉토리로 파일을 옮겨보십시오. – MadProgrammer