-
Finding Text Files
Hi, I'm new to the forums.
I joined as I have a problem with a text file. I'm just testing on an example code, but everytime I try and run the java project it says it can not find the file.
Code:
public class JavaTest {
public static void main(String[] args) {
TextIO.readFile("sample_maze2.txt");
// sample_maze2.txt has a line with two numbers on it saying how many rows and
// how many columns of characters there are in the rest of the file.
// All calls to TextIO.get methods read now read from sample_maze2.txt.
int rows = TextIO.getInt(); // this is the number of rows in the file (maze)
int cols = TextIO.getlnInt(); // this is the number of columns in the file (maze)
char[][] array = new char[6][12];
// Each line of sample_maze2.txt contains cols number of characters
// and there are rows number of lines
for (int i=0; i < rows; i++) {
for (int j=0; j < cols; j++) {
array[i][j] = TextIO.getChar();
}
TextIO.getln();
}
TextIO.readStandardInput(); // This closes the file
TextIO.putln("Display the maze using 'maze' characters:");
// In the maze project, whenever you read a '0' you display a wall character
// whenever you read a '1' you display a path character
// whenever you read a '2' you display an entrance character
// whenever you read a '3' you display an exit character
for (int i=0; i < rows; i++) {
for (int j=0; j < cols; j++) {
if (array[i][j]=='0') TextIO.put('*');
if (array[i][j]=='1') TextIO.put(' ');
if (array[i][j]=='2') TextIO.put('E');
if (array[i][j]=='3') TextIO.put('X');
}
TextIO.putln();
}
}
}
The sample_maze2.txt file is in the same file as the java project I'm making, any reason why I always get "Error :java.io.FileNotFoundException: sample_maze2.txt (The system cannot find the file specified)"?
Quick help would be great, if I solved this, I'd be well on my way!
-
The file should be found if it's in the project root directory. An easy way to find out where that is, is to create a file in your code and see where it gets put, e.g: Code:
TextIO.writeFile("file_test.txt");
TextIO.put("foo!");
-
Thank you for your quick reply, I have managed to find the textfile now, for some reason the folder it was in, was not acceptable.
It's now solved!