Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.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_Resources_Text Files_Filepath_Project Structure - Fatal编程技术网

Java can';在资源文件夹中找不到文本文件

Java can';在资源文件夹中找不到文本文件,java,resources,text-files,filepath,project-structure,Java,Resources,Text Files,Filepath,Project Structure,我似乎无法让Java检测我的文本文件。它一直给我一个FileNotFoundException()我以前已经能够让它找到PNG和TTF文件,但是我不确定我是否需要做一些特殊的事情才能让它找到资源文件夹中的文本文件 我正在使用Eclipse 2018-19(4.9.0)和OpenJDK 11 如何使我的程序能够找到并利用此文本文件 MCV示例: public static main(String[] args) { generateHumanName(); } /** * Generat

我似乎无法让Java检测我的文本文件。它一直给我一个
FileNotFoundException()
我以前已经能够让它找到PNG和TTF文件,但是我不确定我是否需要做一些特殊的事情才能让它找到资源文件夹中的文本文件

我正在使用Eclipse 2018-19(4.9.0)和OpenJDK 11

如何使我的程序能够找到并利用此文本文件

MCV示例:

public static main(String[] args) {
   generateHumanName();
}

/**
 * Generates a male "human-ish" name from a text file of syllables.
 * @return String The male human name.
 */
private static String generateHumanName() {
   try {
      FileReader("/text/male_human_name_syllables.txt/");
   } catch (IOException e) {
      e.printStackTrace();
   }
      return null; // never called
}
收到异常:

java.io.FileNotFoundException:../res/text/male\u human\u name\u sylleles.txt(无此类文件或目录)
位于java.io.FileInputStream.open0(本机方法)
在java.io.FileInputStream.open(FileInputStream.java:195)
位于java.io.FileInputStream。(FileInputStream.java:138)
位于java.io.FileInputStream。(FileInputStream.java:93)
位于java.io.FileReader。(FileReader.java:58)
在mcve.mcve.generateHumanName(mcve.java:21)
位于mcve.mcve.main(mcve.java:12)
我的文件路径:


您正在将此文件放入资源文件夹(
res
):这与在本地文件系统上浏览文件不同(请参见下文):

您需要使用
MCV.class.getResourceAsStream(“/text/male\u human\u name\u sylleles.txt”)

我不会在
getResourceAsStream
上钻研太多,但是:

  • 如果资源不存在,它可能返回
    null
    。您可能需要将测试移到“使用资源尝试”之外
  • Eclipse将任何非java文件复制到输出文件夹中(默认情况下为
    bin
  • 代码末尾的正斜杠是错误的。Java可能尝试读取目录
  • 如果没有在路径(
    /text
    )前加上正斜杠,它将与调用
    getResourceAsStream
    方法的类(和包)相关
  • 或者,如果您希望在某个随机位置读取文件,则应将其传递(例如:配置Eclipse以使用一些参数执行您的程序)到您的程序并读取:

    public static main(String[] args) {
       if (args.length < 1) throw new IllegalArgumentException("missing path");
       generateHumanName(args[0]);
    }
    
    /**
     * Generates a male "human-ish" name from a text file of syllables.
     * @return String The male human name.
     */
    private static String generateHumanName(String path) {
      try (FileReader reader = new FileReader(path)) {
    
      } catch (IOException e) {
        e.printStackTrace();
      }
      return null; // never called
    }
    
    publicstaticmain(字符串[]args){
    如果(args.length<1)抛出新的IllegalArgumentException(“缺少路径”);
    generateHumanName(args[0]);
    }
    /**
    *从音节文本文件中生成男性“人类”名称。
    *@return String男性人名。
    */
    私有静态字符串generateHumanName(字符串路径){
    try(FileReader=newfilereader(路径)){
    }捕获(IOE异常){
    e、 printStackTrace();
    }
    返回null;//从未调用
    }
    
    否则,您必须将
    text
    文件夹移动到项目的根目录(其中
    res
    文件夹),刷新项目,并使用
    text/male\u human\u name\u sylleles.txt
    (因为这是一个绝对路径)

    res/text/male\u human\u name\u sylleles.txt可能会起作用(因为它从项目的根目录执行)

    public static main(String[] args) {
       if (args.length < 1) throw new IllegalArgumentException("missing path");
       generateHumanName(args[0]);
    }
    
    /**
     * Generates a male "human-ish" name from a text file of syllables.
     * @return String The male human name.
     */
    private static String generateHumanName(String path) {
      try (FileReader reader = new FileReader(path)) {
    
      } catch (IOException e) {
        e.printStackTrace();
      }
      return null; // never called
    }