Hi,
I have some code which i want to read many files from a directory using a scanner, i can get it to read a single file, but when i edit the code to find the other files i get a FileNotFound Exception saying it can not find the name of the first file in the list! Here is the code:
public void getInfoFromFiles() throws FileNotFoundException{ File file1 = new File("C:/Users/Todd/Documents/NetBeansProjects/Project/files/"); File[] listOfFiles = file1.listFiles(); for (int i = 0; i < listOfFiles.length; i++) { FileReader file = new FileReader(listOfFiles[i].getName()); // This is the line which is most likely wrong Scanner src = new Scanner(file); while (src.hasNextLine()) { String line = src.nextLine(); System.out.println(line); } } } public static void main(String[] args) throws IOException { stageOne test = new stageOne(); test.getInfoFromFiles();
The Error i get is
Exception in thread "main" java.io.FileNotFoundException: hello.log (The system cannot find the file specified) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.<init>(FileInputStream.java:106) at java.io.FileInputStream.<init>(FileInputStream.java:66) at java.io.FileReader.<init>(FileReader.java:41) at stageOne.getInfoFromFiles(stageOne.java:65) at stageOne.main(stageOne.java:80)
Is there a way to get the scanner to read many files, or a better way to implement this!
Thank you!