Kindly give the absolute path of file in FileReader("absolute path")
.Or place the txt file where your java source file is present.
Can't load in txt file on the command line
-
13-07-2023 - |
Question
I am unable to load in a txt file on the command line but in eclipse it loads fine. Below is the code that loads in the file.
try{
BufferedReader in =
new BufferedReader(new FileReader("piratewords.txt"));
int count = 0;
while (in.ready()) { //while there is another line in the input file
game.puzzles[count] = in.readLine(); //get line of data
count++; //Increment CWID counter
}
in.close();
}
catch(IOException e){
System.out.println("Error during reading/writing");
}
La solution
Autres conseils
piratewords.txt file should be available inside a classpath or else we need to specify
full absolute path of the location where the file is.
looks like when you are running it from command line, you are not keeping this file
in the required classpath.
better give absolute path of the file and check it.
absolute path like below
BufferedReader in = new BufferedReader(
new FileReader("C:/Users/karibasappagc/Desktop/piratewords.txt"));
and make sure file existence in the specified path.
This happens because the file is not in the classpath of your JVM.
Check Eclipse running classpath or use absolute path for your piratewords.txt. The file should at least be in your classes
folder or you should add the folder it's in, in your classpath.