FileReader errors
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: FileReader errors

  1. #1
    Join Date
    Jun 2009
    Posts
    63

    FileReader errors

    hello all,

    I am trying to build and run the code below on netbeans IDE, and for some reason I am getting a error message which I cannot solve. this is exactly how the code appears in multiple tutorials, which is why I am confused. the error message I am getting is:

    run:
    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
    at fileiotest.Main.main(Main.java:25)
    Java Result: 1

    The file that I want to read is definitely in the right place, on my desktop. Here is the code itself:

    package fileiotest;

    import java.io.*;
    import java.util.*;
    import java.io.FileReader;

    public class Main {

    public static void main(String[] args) {

    FileReader fr = new FileReader ("/home/iochinome/Desktop/wdbc.data");

    }

    }



    and strangely enough, it throws the same error message even when i replace the line
    FileReader fr = new FileReader ("/home/iochinome/Desktop/wdbc.data");
    with
    File f = new File("/home/iochinome/Desktop/wdbc.data");
    FileReader fr = new FileReader ("/home/jay/Desktop/wdbc.data");

    Thanks!!!!

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: FileReader errors

    It generally helps if you read the error message before trying to solve the problem. The message says "Uncompilable source code - unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown".

    This has nothing to do with where the file is, the problem is the code can't be compiled. The message goes on to say the named exception must be caught or thrown. This tells you what needs to be done to your code to get it to compile. ie you have to catch and handle the FileNotFoundException or you declare the main method as throwing the exception. For example:

    Code:
    package fileiotest;
    
    import java.io.*;
    import java.util.*;
    import java.io.FileReader;
    
    public class Main throws FileNotFoundException {
    
    public static void main(String[] args) {
    
    FileReader fr = new FileReader ("/home/iochinome/Desktop/wdbc.data");
    
    }
    }
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center