File io 从命令行运行程序时无法读取文件,但它可以从Eclipse运行
我有一个简单的java程序,它在eclipse中运行良好,但在从命令行运行时找不到我读写的.txt文件。我试图更改文件的权限,但因为它们在eclipse中运行,所以这似乎不是问题所在。我没有阅读Java文件的经验。但我认为这是一个路径问题。有人能帮我修改我的脚本或者别的什么东西吗 我得到了一堆这样的东西:File io 从命令行运行程序时无法读取文件,但它可以从Eclipse运行,file-io,File Io,我有一个简单的java程序,它在eclipse中运行良好,但在从命令行运行时找不到我读写的.txt文件。我试图更改文件的权限,但因为它们在eclipse中运行,所以这似乎不是问题所在。我没有阅读Java文件的经验。但我认为这是一个路径问题。有人能帮我修改我的脚本或者别的什么东西吗 我得到了一堆这样的东西: java.io.FileNotFoundException: helloState.txt (No such file or directory) at java.io.FileInp
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)
以下是我打开文件的方式:
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;
}
public LinkedList readByLine(字符串文件名){
文件=新文件(文件名);
FileInputStream fis=null;
BufferedInputStream bis=null;
BufferedReader br=null;
串入;
LinkedList fileLines=新建LinkedList();
试一试{
fis=新文件输入流(文件);
bis=新的缓冲数据流(fis);
br=新的BufferedReader(新的文件读取器(文件));
while(br.ready()){
/*从文本文件中读取该行*/
in=br.readLine();
/*如果该行为空,请停止读取*/
if(in.isEmpty()){
打破
}
/*将该行添加到链接列表中*/
fileLines.add(in);
}
/*使用完所有资源后,请进行处置*/
fis.close();
二、关闭();
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
返回文件行;
}
尝试从src
上方的目录启动它。作为类路径,使用src
谢谢您的回答。从src文件运行脚本会运行代码,但问题是,当我试图打开存储在CS317_A4目录中的任何文本文件时,会出现Java.io.FileNotFound异常。@Michael:不是从src。从上面的文件夹。谢谢你的额外澄清,这解决了问题。我一开始误解了你的答案。
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;
}