The Artima Developer Community
Sponsored Link

Java Answers Forum
BufferedReader driving me nuts!!

6 replies on 1 page. Most recent reply: Jan 27, 2004 11:11 PM by Jitender

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
Laurence

Posts: 5
Nickname: lau03
Registered: Feb, 2003

BufferedReader driving me nuts!! Posted: Feb 2, 2003 8:44 PM
Reply to this message Reply
Advertisement
hi everyone,
I just started learning Java a couple of weeks ago, so please bear with my (most certainly) stupid question:
I just can't figure out how to use BufferedReader.
Here's what I've got:


void readFile(String args[]) {
BufferedReader in = new BufferedReader(new InputStreamreader (args[0]));
} // end of method readFile



and I keep getting this compiling error:
Solution.java:44: cannot resolve symbol
symbol : class InputStreamreader
location: class Solution
BufferedReader in = new BufferedReader(new InputStreamreader (args[0]));
^
1 error

I'm not getting it... really!!
I imported java.io.*; correct? so that can't be the problem. If someone could just help me out here!!

Thanks


Laurence

Posts: 5
Nickname: lau03
Registered: Feb, 2003

Re: BufferedReader driving me nuts!! Posted: Feb 2, 2003 8:48 PM
Reply to this message Reply
One more thing (if that makes a difference here!!):
the file I'm trying to read is a text file.

Laurence

Posts: 5
Nickname: lau03
Registered: Feb, 2003

Re: BufferedReader driving me nuts!! Posted: Feb 2, 2003 9:16 PM
Reply to this message Reply
I think I sort of figured out what was hapenning, but I'm still confused:

I re-wrote my method as follows:

void readFile(String fileName) {

FileInputStream file = new FileInputStream(fileName);
BufferedReader in = new BufferedReader( new InputStreamReader(file));

String line = new String();
int num = -1;
do {
num = Integer.parseInt(line);
} while (num != -1);
System.out.println("line = " + line);

} // end of method readFile


By the way, I'm taking in a command-line argument. In main, I declared: String fileName = new String(args[0]);

Now, I'm getting
Solution.java:45: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
FileInputStream file = new FileInputStream(fileName);
^
1 error
as an error message!! Why can't it find the file ??

Mike Penner

Posts: 16
Nickname: mikepenner
Registered: Jan, 2003

Re: BufferedReader driving me nuts!! Posted: Feb 3, 2003 9:52 AM
Reply to this message Reply
> I think I sort of figured out what was hapenning, but I'm
The first problem was that you had a lower-case "r" (InputStreamreader) instead of an upper-case "r" (InputStreamReader). The Java compiler treats class names as case-sensitive.

The second problem occurs because the FileInputStream constructor can throw a FileNotFoundException. To catch this exception, do something like:

try{
// The code that can throw an exception
} catch(Exception e){
e.printStackTrace(); // handle the exception somehow
}


Also check out http://java.sun.com/docs/books/tutorial/java/nutsandbolts/exception.html for more detailed information about the Java exception handling process.

Your last comment also makes me think you're confusing compile-time and run-time errors. You describe a compile-time error (a declared but uncaught and unthrown exception). The variable that holds the name of the file (passed in from the command line) will not exist until run-time, so the problem is not that it can't find the file you're trying to read.

Jo

Posts: 3
Nickname: jodedor
Registered: Feb, 2003

Re: BufferedReader driving me nuts!! Posted: Feb 3, 2003 11:56 AM
Reply to this message Reply
> I think I sort of figured out what was hapenning, but I'm
> still confused:
>
> I re-wrote my method as follows:
>
> void readFile(String fileName) {
>
> FileInputStream file = new FileInputStream(fileName);
> BufferedReader in = new BufferedReader( new
> new InputStreamReader(file));
>
> String line = new String();
> int num = -1;
> do {
> num = Integer.parseInt(line);
> } while (num != -1);
> System.out.println("line = " + line);
>
> } // end of method readFile
>

>
> By the way, I'm taking in a command-line argument. In
> main, I declared: String fileName = new String(args[0]);
>
> Now, I'm getting
> Solution.java:45: unreported exception
> java.io.FileNotFoundException; must be caught or declared
> to be thrown
> FileInputStream file = new FileInputStream(fileName);
> ^
> 1 error
> as an error message!! Why can't it find the file ??

You should declare the exception as in:

public static void main(String args[]) throws IOException {

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: BufferedReader driving me nuts!! Posted: Feb 3, 2003 3:14 PM
Reply to this message Reply
import java.io.*;
 
public class Test{
    
    public Test(){
        
    }
    
    public static void main (String[] args){
        Test test = new Test();
        test.readFile("test.txt");
    }
    
    
    public String readFile(String fileName){
        String fileText = "";
        try{
            String lineRead = "";
            File file = new File(fileName);
            FileReader fr = new FileReader(file); 
            BufferedReader br = new BufferedReader(fr);
            while ((lineRead = br.readLine()) != null){
                fileText = fileText + lineRead + "\n";
            }
        }catch(FileNotFoundException fnfe){
            System.err.println("FileNotFoundException: " + fnfe.getMessage());
        }catch(IOException ioe){
            System.err.println("IOException: " + ioe.getMessage());
        }
        return fileText;        
    }
}

Jitender

Posts: 1
Nickname: jiten
Registered: Jan, 2004

Re: BufferedReader driving me nuts!! Posted: Jan 27, 2004 11:11 PM
Reply to this message Reply
sir,
u have 2 issues:
1) u are intermixing the input of file name with the actual reading of the file' So. u must use FileReader instead of InputStreamReader
2) Also, as u were then new to Java, hence didn'e know that a class of errors(beyond ur control) are to be definitely catched as the JVM is built up like that and these type of errors are predefined and either must be caught or thrown(u can bluff up here and throw in thin air, i.e. just throw just like an empty catch block, to be caught nowhere , provided no error u feel of that sort can occur).
So, coming back u must catch these errors.
Yours
Jiten

Flat View: This topic has 6 replies on 1 page
Topic: Library to get information on video files? Previous Topic   Next Topic Topic: question on  StreamTokenizer  class

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use