Java-终端中的FileNotFoundException
我使用的这个方法在eclipse中运行得很好Java-终端中的FileNotFoundException,java,Java,我使用的这个方法在eclipse中运行得很好 public boolean verifyAdminLoginCredentials(String keyboardInputUsername, String keyboardInputPassword) { try { br = new BufferedReader(new FileReader("adminInfo.txt")); while ((line = br.readLine()) != null
public boolean verifyAdminLoginCredentials(String keyboardInputUsername, String keyboardInputPassword) {
try {
br = new BufferedReader(new FileReader("adminInfo.txt"));
while ((line = br.readLine()) != null) {
StringTokenizer stringTokenizer = new StringTokenizer(line, "|");
while (stringTokenizer.hasMoreElements()) {
usernameInFile = stringTokenizer.nextElement().toString();
passwordInFile = stringTokenizer.nextElement().toString();
if((keyboardInputUsername.equals(usernameInFile) && (passwordHSHA256(keyboardInputPassword).equals(passwordInFile)))) {
isLogin = true;
break;
}
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
return isLogin;
}
但当我编译所有java文件并在macbook终端上运行时。这给我带来了一个错误
程序找不到该文件
java.io.FileNotFoundException: adminInfo.txt (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:146)
at java.io.FileInputStream.<init>(FileInputStream.java:101)
at java.io.FileReader.<init>(FileReader.java:58)
at UserAccount.verifyAdminLoginCredentials(UserAccount.java:97)
java.io.FileNotFoundException:adminInfo.txt(没有这样的文件或目录)
在java.io.FileInputStream.open(本机方法)
位于java.io.FileInputStream。(FileInputStream.java:146)
位于java.io.FileInputStream。(FileInputStream.java:101)
位于java.io.FileReader。(FileReader.java:58)
在UserAccount.verifyAdminLoginRedentials(UserAccount.java:97)中
为什么会这样?请帮助您在终端中运行的目录是什么
$ pwd
/my/dir
$ ls
MyClass.java MyClass.class adminInfo.txt
确保在列出目录时,adminInfo.txt
文件存在。否则,您可能需要更改目录布局。通常,在eclipse中,您会遇到如下情况:
Project
/src (sources {.java} files here)
/bin (binaries {.class} files here)
adminInfo.txt
因此,当您在Eclipse中运行应用程序时,/bin
会自动添加到您的类补丁中
因此,如果您在终端中转到项目目录,您可以尝试以下操作:
$ cd MY_PROJECT_DIR
$ java -cp ./bin MyClass
更新:现在我知道目录名了
试试这个:
$ cd /Users/gerard/Documents/workspace/sampleprogram
$ java -cp ./bin PUT_THE_NAME_OF_YOUR_CLASS_HERE
问题似乎是文件
adminInfo.txt
不在运行程序的目录中。要查找当前目录类型pwd
,并查看该目录中的文件,请使用命令ls
。如果要指定要读取的文件不在程序目录中,则必须指定完整路径(即,将adminInfo.txt
替换为“/Users/gerard/Documents/workspace/sampleprogram/adminInfo.txt”
)您需要:
br = new BufferedReader(new FileReader("/Users/gerard/Documents/workspace/sampleprogram/adminInfo.txt"));
而不是
br = new BufferedReader(new FileReader("adminInfo.txt"));
从Eclipse执行时,具有相对路径名的文件(即,它不以“/”开头)位于Eclipse项目的根目录下。但如果从终端执行,则相同的相对路径指向“工作目录”中及其下方的某个位置。使用
pwd
查看此目录,使用cd
更改此目录
之后
在/Users/gerard/Documents/workspace/sampleprogram/src中,输入命令
cd ..
然后使用
java -cp src xxx
其中xxx是您在运行java
之后使用的任何东西,请替换此
br = new BufferedReader(new FileReader("adminInfo.txt"));
与
ie具有文件的完全限定名
如果仍然出现异常,您就有麻烦了。很可能您运行该文件的目录中不存在该文件。你是如何运行程序的?文件是否存在?是的,文件存在。文件存在于何处?这与你运行程序的方式无关。请将所有信息添加到问题中。您可能希望使用文件的完全限定名。我将文本文件放置在/Users/gerard/Documents/workspace/sampleprogram中
br = new BufferedReader(new FileReader("adminInfo.txt"));
br = new BufferedReader(new FileReader("/Users/gerard/Documents/workspace/sampleprogram/adminInfo.txt"));