-1

I have a method readFromFile like this:

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;
}

and I use this method here to read a folder of txt files. I need it to put the names of files in an arraylist (the files are an alphabet):

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;
}

The source folder includes files like: A.txt, B.txt, etc. When I try to run the code I get an error.

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)

I guess the method can't find the files on my computer. But I don't know why. Is the form wrong? Can you help me?

  • please read the java documentation of getResourceAsStream method where it is mentioned as below "Finds a resource with a given name. The rules for searching resources associated with a given class are implemented by the defining class loader of the class. This method delegates to this object's class loader. If this object was loaded by the bootstrap class loader, the method delegates to ClassLoader.getSystemResourceAsStream." – Aarish Ramesh Feb 14 '18 at 10:37

2 Answers2

2

You are passing an absolute file path to Class.getResourceAsStream(...) which is wrong.

Option 1: Use Class.getResourceAsStream(...) but use a path which is relative to a folder (or jar) which is on the classpath. Eg if C:\\Users\\hp\\Desktop\\resources is a folder on the classpath you could do

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

Option 2: Use FileInputStream to read from a full file path

new FileInputStream("C:\\Users\\hp\\Desktop\\resources\\" + letter + ".txt")
lance-java
  • 25,497
  • 4
  • 59
  • 101
0

Class.getResourceAsStream is for (read-only) resources on the class path, possibly zipped in a jar.

Replace

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

by

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

The class Files provides nice methods, like for getting all lines.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138