Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 未找到文件异常-Can';我看不出问题所在_Java_Java Io_Filenotfoundexception - Fatal编程技术网

Java 未找到文件异常-Can';我看不出问题所在

Java 未找到文件异常-Can';我看不出问题所在,java,java-io,filenotfoundexception,Java,Java Io,Filenotfoundexception,我尝试过使用整个路径直接链接,但也没有解决它 package eliza; import java.io.*; public class Eliza { public static void main(String[] args) throws IOException { String inputDatabase = "src/eliza/inputDataBase.txt"; String outputDatabase = "src/eliza/outputDataBas

我尝试过使用整个路径直接链接,但也没有解决它

package eliza;

import java.io.*;

public class Eliza {

public static void main(String[] args) throws IOException {
    String inputDatabase = "src/eliza/inputDataBase.txt";
    String outputDatabase = "src/eliza/outputDataBase.txt";
    Reader database = new Reader();

    String[][] inputDB = database.Reader(inputDatabase);
    String[][] outputDB = database.Reader(outputDatabase);

}
}
以下是reader类:

package eliza;

import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;

public class Reader {

public String[][] Reader(String name) throws IOException {
    int length = 0;
    String sizeLine;

    FileReader sizeReader = new FileReader(name);
    BufferedReader sizeBuffer = new BufferedReader(sizeReader);

    while((sizeLine = sizeBuffer.readLine()) != null) {
        length++;
    }

    String[][] database = new String[length][1];

    return (database);
}
}
这是我的通讯录的照片。我甚至将这些文本文件放在“eliza”根文件夹中:


有什么想法吗?

给出准确的路径,如

String inputDatabase = "c:/java/src/eliza/inputDataBase.txt";

您没有提供正确的路径,请重新检查

试一试


因为您使用的是IDE,所以需要给出完整的规范路径。应该是

String inputDatabase = "C:\\Users\\Tommy\\Desktop\\Eliza\\src\\eliza\\inputDataBase.txt";
String outputDatabase = "C:\\Users\\Tommy\\Desktop\\Eliza\\src\\eliza\\outputDataBase.txt";

IDE可能正在从其
bin
文件夹执行字节码,并且找不到相对引用。

源目录树在执行期间通常不存在,因此运行时需要的文件不应放在那里。。。除非您要将它们用作资源,否则在这种情况下,它们的路径名与包根相对,并且不以“src”开头,并且数据是通过getResourceXXX()方法访问的,而不是通过FileInputStream访问的。

文件引用将与程序的执行上下文相对。将文件放在src文件夹中会生成嵌入的资源,这是另一个问题。添加System.out.println(新文件(“.”.getCanonicalPath());到程序的开始,它将告诉程序运行的“位置”…C:\Users\Tommy\Desktop\Eliza\src这是结果。那么我应该可以直接使用inputDataBase.txt了,是吗?我解决了这个问题。我在源目录的eliza package文件夹中有这些文件。我把它们移到了src,问题就解决了。有什么方法可以引用打包目录中的文件吗?我担心它在src目录中运行,但是是的,您可以将它放在这里。我假设您在eclipse中运行……点缀在应用程序类路径上下文中的文件通常称为嵌入式资源。可以使用诸如getClass().getResource(“…”)之类的方法来访问它们。这将返回一个指向资源的URL,您可以访问该资源的InputStream来读取其内容……更好的(可移植的)解决方案是解析工作目录;)你这么说很有趣,因为我的文件也在Eliza根目录中。我甚至将它们复制到IDE生成的每个文件夹中:build、dist、nbproject、src。什么样的方法可以使它可移植,这样我就不必指定完整的规范路径?编辑:没有注意到你关于可移植性的评论。谢谢。@user2318481编写一个包含所有JAR和java文件的小脚本,并从命令行编译。然后从根目录运行主文件。所有相关引用都将从该目录解析。“C://Users//Tommy//Desktop//Eliza//src//Eliza//outputDataBase.txt”“C://Users//Tommy//Desktop//Eliza//src//Eliza inputDataBase.txt”我使用了它并解决了问题。不过我需要把这个便携式的。有什么建议吗?@user2318481便携式是什么意思?
String inputDatabase = "C:\\Users\\Tommy\\Desktop\\Eliza\\src\\eliza\\inputDataBase.txt";
String outputDatabase = "C:\\Users\\Tommy\\Desktop\\Eliza\\src\\eliza\\outputDataBase.txt";