Java – cannot find files using FileReader
•
Java
I'm developing a project for my course. We should read one called samplesearch Txt file, the following is the code I am using The problem is that I can't really load files through eclipse Even if the text file is in the same directory as the rest of the code, it always appears file not found
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");
}
}
}
Solution
You need to know where the code searches for files:
Do this:
Get current path: system out. println(new File(“.”). getAbsoluteFile());
This will show the path where the Java code looks for the file Use a relative path from this location to reference your file Cheers!
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
