Simple enough, I'm attempting to read a text file that is compiled in the jar that is being run.
so the file structure would look something like this
test.jar
-main.class
-META.INF
--MAINFEST.MF
-lists
--readfrom.txt
I've looked around a saw someone had a similar issue to me, Reading File In JAR using Relative Path I attempted that but got an error. Here is what I adapted from it
List<String> testList = new ArrayList<>();
try {
InputStream configStream = getClass().getResourceAsStream("lists\\readfrom.txt");
BufferedReader configReader = new BufferedReader(new InputStreamReader(configStream, "UTF-8"));
File file = new File(String.valueOf(configReader));
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
testList.add(scanner.nextLine());
}
scanner.close();
}
I ran that and got the error
java.io.FileNotFoundException: java.io.BufferedReader@4cf57088 (The system cannot find the file specified)
Is there anything I'm missing or this the wrong approach?
This is what I have so far
List<String> testList = new ArrayList<>();
try {
//what ever needs to happen
File file = new File(filepath);
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
testList.add(scanner.nextLine());
}
scanner.close();
}
I just want this to read the file and add each line as a new item to the list.