【问题标题】:My method readFromFiles can't find the source files [duplicate]我的方法 readFromFiles 找不到源文件 [重复]
【发布时间】:2018-02-14 10:22:37
【问题描述】:

我有一个这样的方法 readFromFile:

private static ArrayList<ArrayList<Integer>> readFromFile(String name) {
ArrayList<ArrayList<Integer>> inputs = new ArrayList<ArrayList<Integer>>();

try {
    InputStream in = ReadWriteFile.class.getClass().getResourceAsStream(name);
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    String line;

    while ((line = reader.readLine()) != null) {
        ArrayList<Integer> input = new ArrayList<Integer>();

        for (int i=0; i<line.length(); i++) {
            int value = 0;
            try {
                value = Integer.parseInt(String.valueOf(line.charAt(i)));
            } catch (Exception e) {
                input.add(value);
            }
        inputs.add(input);
        }
    reader.close();
    }
} catch (IOException e) {
    e.printStackTrace();
}
return inputs;
}

我在这里使用这个方法来读取一个txt文件的文件夹。我需要将文件名放入数组列表中(文件是字母表):

public static ArrayList<TrainingSet> readTrainingSets() {
ArrayList<TrainingSet> trainingSets = new ArrayList<TrainingSet>();

for (int i=0; i<26; i++) {
    char letterValue = (char) (i+26);
    String letter = String.valueOf(letterValue);

    for (ArrayList<Integer> list : readFromFile("C:\\Users\\hp\\Desktop\\resources\\" + letter + ".txt")) {
        trainingSets.add(new TrainingSet(list, GoodOutputs.getInstance().getGoodOutput(letter)));
    }
}
return trainingSets;
}

源文件夹包括以下文件:A.txt、B.txt 等。当我尝试运行代码时出现错误。

Exception in thread "main" java.lang.NullPointerException
at java.io.Reader.<init>(Unknown Source)
at java.io.InputStreamReader.<init>(Unknown Source)
at data.ReadWriteFile.readFromFile(ReadWriteFile.java:34)
at data.ReadWriteFile.readTrainingSets(ReadWriteFile.java:22)
at network.Train.<init>(Train.java:16)
at gui.MainGui.<init>(MainGui.java:38)
at gui.MainGui.main(MainGui.java:32)

我猜该方法在我的计算机上找不到文件。但我不知道为什么。表格错了吗?你能帮帮我吗?

【问题讨论】:

  • 请阅读 getResourceAsStream 方法的java文档,其中提到如下“查找具有给定名称的资源。搜索与给定类关联的资源的规则由定义类加载器实现类。此方法委托给此对象的类加载器。如果此对象由引导类加载器加载,则该方法委托给 ClassLoader.getSystemResourceAsStream。"

标签: java nullpointerexception inputstream


【解决方案1】:

您将绝对文件路径传递给Class.getResourceAsStream(...),这是错误的。

选项1:使用Class.getResourceAsStream(...),但使用相对于类路径上的文件夹(或jar)的路径。例如,如果C:\\Users\\hp\\Desktop\\resources 是类路径上的一个文件夹,您可以这样做

ReadWriteFile.class.getResourceAsStream("/" + letter + ".txt");

选项 2:使用 FileInputStream 从完整文件路径读取

new FileInputStream("C:\\Users\\hp\\Desktop\\resources\\" + letter + ".txt")

【讨论】:

    【解决方案2】:

    Class.getResourceAsStream 用于类路径上的(只读)资源,可能压缩在 jar 中。

    替换

    try {
        InputStream in = ReadWriteFile.class.getClass().getResourceAsStream(name);
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        ...
        reader close();
    }
    

    通过

    try (BufferedReader = Files.newBufferedReader(Paths.get(name), Charset.getDefaultCharset())) {
        ...
    }
    

    Files 类提供了很好的方法,比如获取所有行。

    【讨论】:

      猜你喜欢
      • 2015-06-08
      • 2016-04-07
      • 2017-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-12
      • 2023-04-10
      相关资源
      最近更新 更多