I am trying to open a text file in my java program but in the console it says Ma
ID: 3903693 • Letter: I
Question
I am trying to open a text file in my java program but in the console it says Maze.txt (The system cannot find the file specified). I am not sure if it is where the text file is in my folders or if it is in my code.
I open the text file in my Maze class:
package kristenalbrechtfinalproject;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
/**
*
* @author Kristen Albrecht
*/
public final class Maze {
static final int maxMazeRow = 20;
static final int maxMazeColumn = 50;
static char[][] maze;
String fileName = "Maze.txt";
Maze(){
Maze.maze = new char[maxMazeRow][maxMazeColumn];
create(fileName);
}
void create(String fileName){
try {
String line;
BufferedReader br;
br = new BufferedReader(new FileReader(fileName));
int num = 0;
while ((line = br.readLine()) != null) {
maze[num] = line.toCharArray();
num++;
if (num > maxMazeRow){
break;
}
}
br.close();
} catch (FileNotFoundException e){
System.out.println(e.getMessage());
} catch (IOException e){
System.out.println(e.getMessage());
}
}
static void display(){
for (char[] mazeRow : maze) {
System.out.println(mazeRow);
}
}
static boolean avaliable(int row, int col){
if ( row >= maxMazeRow || col >= maxMazeColumn){
return false;
}
if ( row < 0 || col < 0){
return false;
}
return (maze[row][col] != '*');
}
}
File structure
Explanation / Answer
This is giving FileNotFoundException because you have pleaced the file in a folder named kristenalbrechtfinalproject and this folder is in src.
So you can do one out of these two things.
1. please the file outside the src folder. i.e. place the file at root of the project folder , at the same place where you have your manifest.mf file. Then your program will work without making any code change.
if you dont want to do this ,then you can do the second option.
2. change the line
String fileName = "Maze.txt";
to
String fileName = "src/kristenalbrechtfinalproject/Maze.txt";
Please double check the spelling and casing of above bold line with the package name you have.
Please let me know if you still face this issue
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.