opening file


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: opening file

  1. #1
    Join Date
    Oct 2004
    Posts
    21

    opening file

    Ive been trying to use the FileReader class to open a file called input.in, but it can't find the file. It is located in the same directory as my java file, is this where it should be?

    Im using code like this:
    FileReader fr = new FileReader ("input.in");

  2. #2
    Join Date
    Sep 2004
    Posts
    223
    show us the error please, also you may want to read this site about reading in files:

    http://www.devdaily.com/java/edu/pj/...pj010017.shtml

    OR

    http://java.sun.com/docs/books/tutorial/essential/io/
    A kram a day keeps the doctor......guessing

  3. #3
    Join Date
    Oct 2004
    Posts
    21
    yea i actually looked at both of those articles when i was trying to write the code to open the file. Here is the error:
    Unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
    FileReader fr = new FileReader ("input.in");

    And then there is a arrow/hat pointing to the n on new.

    Here is my code if it helps:
    PHP Code:
    import java.io.*;

    class 
    ride {
        
        public static 
    void main(String args[])
        {
             
            
    FileReader fr     = new FileReader("input.in");
            
    BufferedReader br = new BufferedReader(fr);
            
    String record null;
        
            
    char[] charLetters = {'A''B''C''D''E''F''G''H''I''J',
                                
    'K''L''M''N''O''P''Q''R''S''T',
                                
    'U''V''W''X''Y''Z'};
            try
            {
                   while ( (
    record=br.readLine()) != null )
                {
                      
    char[] charInput record.toCharArray();
                    
    int[] no = new int[charInput.length];
                
                    for(
    int i=0i<charInput.lengthi++)
                    {
                        for(
    int j=1j<=26j++)
                        {
                            if(
    charInput[i] == charLetters[j])
                            {
                                
    no[i] = j;
                                
    System.out.println(" " no[i]);
                            }
                        }
                    }
                   }

            } catch (
    IOException e) {System.out.println("Error:"+e) ; }
        }


  4. #4
    Join Date
    Sep 2004
    Posts
    223
    ok well first of all, java FORCES you to do I/O checking,

    so you must do it within try/catch statements:

    try{
    // fileopen stuff
    }catch(IOException e){
    System.out.println("error opening file ");
    }

    try that, im not sure if its becuase you havent done it correclty or it cant find the file, but java must have try/catch stuff when dealing with I/O
    A kram a day keeps the doctor......guessing

  5. #5
    Join Date
    Oct 2004
    Posts
    21
    ah it works now!! thanks very much

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center