Cannot read a file when running program from commandline, but it works from Eclipse
I have a simple java program that runs fine in eclipse but cannot find the .txt files I read from and write to when r开发者_JAVA百科un from command line. I tried changing the permissions of the files but because they run in eclipse it seems that is not the issue. I'm not experienced in reading from files in Java. But I think it is a path issue or something. Can anyone help me fix my script or whatever so it works?
I get a bunch of these:
java.io.FileNotFoundException: helloState.txt (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at bot.FileRead.readByLine(FileRead.java:33)
at bot.BuildStates.buildStates(BuildStates.java:16)
at bot.Kate.main(Kate.java:98)
My file structure is as follows CS317_A4/src/myPackage/(class and source files)
My text files are in the CS317_A4 directory and my script is in the src directory (I can't seem to run the program from the CS317_A4 directory
Here is my script for running the program:
#!/bin/bash
set classpath=
java -cp .:.. bot.Kate
Here is how I open the file:
public LinkedList<String> readByLine(String filename) {
File file = new File(filename);
FileInputStream fis = null;
BufferedInputStream bis = null;
BufferedReader br = null;
String in;
LinkedList<String> fileLines = new LinkedList<String>();
try {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
br = new BufferedReader(new FileReader(file));
while(br.ready()){
/* read the line from the text file */
in = br.readLine();
/* if the line is empty stop reading */
if(in.isEmpty()){
break;
}
/* add the line to the linked list */
fileLines.add(in);
}
/* dispose all the resources after using them. */
fis.close();
bis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return fileLines;
}
Try to start it from the directory that's above src
. As classpath, use src
.
精彩评论