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.


>> REGISTER NOW TO START POSTING


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

Results 1 to 4 of 4

Thread: FileNotFoundException-Unable to read the contents of all the files

  1. #1
    Member
    Join Date
    Dec 2013
    Posts
    32
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default FileNotFoundException-Unable to read the contents of all the files

    Hi everyone currently i am working on an assignment of making an Java program to read all the files present in the path and build up a dictionary of words with frequencies of words occurred in the text files but i am getting errors in the reading of files and i am posting the block of code i have used can any one kindly guide me to do this task please!
    public static void build() {
     
    		BufferedReader br = null;
    		Scanner in = new Scanner(System.in);
    		String path_name=in.nextLine();
    		System.out.println("I am building an Dictionary from the files present in the path:"+path_name);
    		Path path = Paths.get(path_name);
    		File folder = new File(path_name);
    		File[] listOfFiles = folder.listFiles();
    		for (int i = 0; i < listOfFiles.length; i++) 
      {
     
       if (listOfFiles[i].isFile()) 
       {
       String files = listOfFiles[i].getName();
           if (files.endsWith(".txt") || files.endsWith(".TXT"))
           {
     
              br = new BufferedReader(new FileReader(files));
    	  String s="";
    	  while(br.ready()) {
    		s+=br.readLine()+"\n";
    					}
            }
    }
    }
    }
    The following is the error i am getting
    Dictionary.java:78: error: unreported exception FileNotFoundException; must be caught or declared to be thrown
    br = new BufferedReader(new FileReader(files));
    ^
    Dictionary.java:80: error: unreported exception IOException; must be caught or declared to be thrown
    while(br.ready()) {
    ^
    Dictionary.java:81: error: unreported exception IOException; must be caught or declared to be thrown
    s+=br.readLine()+"\n";
    ^
    3 errors


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    3,446
    My Mood
    Mellow
    Thanks
    86
    Thanked 476 Times in 467 Posts

    Default Re: FileNotFoundException-Unable to read the contents of all the files

    The error message clearly describes the remedy: put the offending code in a try/catch block or modify the method signature to indicate that the exception may be thrown. You can learn about these details in the Exceptions Trail.

  3. #3
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: FileNotFoundException-Unable to read the contents of all the files

    Quote Originally Posted by Dinesh Raja View Post
    br = new BufferedReader(new FileReader(files));
    String s="";
    while(br.ready()) {
    	s+=br.readLine()+"\n";
    }
    Here in this code there are at least two things that are not really good.
    First, don't use ready(). It has a specific meaning (see documentation, that states: "Returns: True if the next read() is guaranteed not to block for input"). It is rarely useful.
    If you want to detect the end-of-file, you must be aware that readLine() returns null on EOF. There is a common idiom: call readLine(), assign to a variable and test for null, all this into a while condition:

    String line;
    while((line = br.readLine()) != null) {
        // use line for whatever you want
    }

    Second: your final intention is to concatenate all lines (with \n as line separator). Ok, but you have used the string concatenation with += that is inefficient if you have tons of lines. It would be better to append lines and \n into a StringBuffer (or StringBuilder, Java 5+).
    Andrea, www.andbin.net SCJP 5 (91%) SCWCD 5 (94%)

    Useful links for Java beginners My new project Java Examples on Google Code

  4. The Following User Says Thank You to andbin For This Useful Post:

    Dinesh Raja (December 5th, 2013)

  5. #4
    Member
    Join Date
    Dec 2013
    Posts
    32
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: FileNotFoundException-Unable to read the contents of all the files

    Thank you i have solved the problem by using try catch block and i have used readLine() instead of ready() so now its working cool!

Similar Threads

  1. Where did my char(s) go?! FileReader read(CharBuffer target) throwing file contents away...?
    By TooOldForThisStuff in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: May 22nd, 2013, 02:38 PM
  2. Replies: 1
    Last Post: April 26th, 2013, 02:38 AM
  3. Unable to read '#' value using getParameter
    By napster12 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 27th, 2012, 05:06 AM
  4. Web browser unable to read javascript etc.
    By Maloto970 in forum Java Networking
    Replies: 1
    Last Post: February 21st, 2012, 08:11 AM
  5. How to read from all files in a directory & plot the contents?
    By 123 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: February 11th, 2010, 11:43 AM