Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Reading many files using a scanner

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Thanked 0 Times in 0 Posts

    Default Reading many files using a scanner

    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();
        public static void main(String[] args) throws IOException {
            stageOne test = new stageOne();

    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!
    Last edited by jayjames90; October 22nd, 2009 at 04:22 PM.

  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Thanked 833 Times in 772 Posts
    Blog Entries

    Default Re: Reading many files using a scanner

    Try using getAbsolutePath() instead of getName(). getName() returns the name of the file only eg no path information to the file
    Last edited by copeg; October 22nd, 2009 at 04:36 PM.

  3. The Following User Says Thank You to copeg For This Useful Post:

    jayjames90 (October 22nd, 2009)

  4. #3
    Junior Member
    Join Date
    Oct 2009
    Thanked 0 Times in 0 Posts

    Default Re: Reading many files using a scanner

    Thank you very much! That worked!

Similar Threads

  1. Java program which can list all files in a given directory
    By JavaPF in forum Java Programming Tutorials
    Replies: 8
    Last Post: July 9th, 2013, 03:38 AM
  2. exe files
    By subhvi in forum Java Theory & Questions
    Replies: 17
    Last Post: September 3rd, 2009, 09:43 AM
  3. reading a char with SCANNER
    By lotus in forum Java SE APIs
    Replies: 6
    Last Post: July 29th, 2009, 05:03 AM
  4. Reading IEEE float values from files.
    By username9000 in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: June 30th, 2009, 12:56 PM
  5. [SOLVED] Using 3rd Party JAR Files
    By oss_2008 in forum Java Theory & Questions
    Replies: 12
    Last Post: June 12th, 2009, 10:27 AM