Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 722
  • Last Modified:

BufferedReader: cannot resolve symbol

Hi,
I'm a begginer with Java and I have a problem when I try to compile a test class. I've been looking for a solution reading a lot of messages sent to forums, but I don't identify the solution. The problem is:

-------------------------------- The code:

import java.io.*;
public class prueba {
   
    public void main() {
        int i;
        i = fileCopy();
    }
   
    public int fileCopy() {
        BufferedReader entrada = new BufferedReader(new FileReader("texto.txt"));
        PrintWriter salida = new PrintWriter(new FileWriter("tocoto.txt"), true);
        String line;
        while((line = entrada.readLine()) != null) {
            salida.println(line);
        }
        entrada.close();
        salida.close();
        return 1;
    }
} // -- end class prueba

--------------------------- The compilation output:
Command line:
javac -classpath ".;C:\Documents and Settings\rafa\Mis documentos\MisWebs\Java\prueba1" -g -verbose "C:\Documents and Settings\rafa\Mis documentos\MisWebs\Java\prueba1\prueba.java"

[parsing started C:\Documents and Settings\rafa\Mis documentos\MisWebs\Java\prueba1\prueba.java]
[parsing completed 110ms]
[checking prueba]
[loading C:\Archivos de programa\Java\j2re14101\lib\rt.jar(java/lang/Object.class)]
[loading C:\Archivos de programa\Java\j2re14101\lib\rt.jar(java/io/BufferedReader.class)]
[loading C:\Archivos de programa\Java\j2re14101\lib\rt.jar(java/io/FileReader.class)]
[loading C:\Archivos de programa\Java\j2re14101\lib\rt.jar(java/io/FileDescriptor.class)]
[loading C:\Archivos de programa\Java\j2re14101\lib\rt.jar(java/lang/String.class)]
[loading C:\Archivos de programa\Java\j2re14101\lib\rt.jar(java/io/File.class)]
[loading C:\Archivos de programa\Java\j2re14101\lib\rt.jar(java/io/InputStreamReader.class)]
[loading C:\Archivos de programa\Java\j2re14101\lib\rt.jar(java/io/InputStream.class)]
[loading C:\Archivos de programa\Java\j2re14101\lib\rt.jar(java/io/Reader.class)]
[loading C:\Archivos de programa\Java\j2re14101\lib\rt.jar(java/io/PrintWriter.class)]
[loading C:\Archivos de programa\Java\j2re14101\lib\rt.jar(java/io/FileWriter.class)]
[loading C:\Archivos de programa\Java\j2re14101\lib\rt.jar(java/io/OutputStreamWriter.class)]
[loading C:\Archivos de programa\Java\j2re14101\lib\rt.jar(java/io/OutputStream.class)]
[loading C:\Archivos de programa\Java\j2re14101\lib\rt.jar(java/io/Writer.class)]
[loading C:\Archivos de programa\Java\j2re14101\lib\rt.jar(java/lang/Error.class)]
[loading C:\Archivos de programa\Java\j2re14101\lib\rt.jar(java/io/FileNotFoundException.class)]
[loading C:\Archivos de programa\Java\j2re14101\lib\rt.jar(java/io/IOException.class)]
[loading C:\Archivos de programa\Java\j2re14101\lib\rt.jar(java/lang/Exception.class)]
[loading C:\Archivos de programa\Java\j2re14101\lib\rt.jar(java/lang/Throwable.class)]
[loading C:\Archivos de programa\Java\j2re14101\lib\rt.jar(java/lang/RuntimeException.class)]
C:\Documents and Settings\rafa\Mis documentos\MisWebs\Java\prueba1\prueba.java:11: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
        BufferedReader entrada = new BufferedReader(new FileReader("texto.txt"));
                                                    ^
C:\Documents and Settings\rafa\Mis documentos\MisWebs\Java\prueba1\prueba.java:12: unreported exception java.io.IOException; must be caught or declared to be thrown
        PrintWriter salida = new PrintWriter(new FileWriter("tocoto.txt"), true);
                                             ^
C:\Documents and Settings\rafa\Mis documentos\MisWebs\Java\prueba1\prueba.java:14: unreported exception java.io.IOException; must be caught or declared to be thrown
        while((line = entrada.readLine()) != null) {
                             ^
C:\Documents and Settings\rafa\Mis documentos\MisWebs\Java\prueba1\prueba.java:17: unreported exception java.io.IOException; must be caught or declared to be thrown
        entrada.close();
               ^
[total 3154ms]
4 errors

--------------------------- The CLASSPATH value:

.;C:\Archivos de programa\Java\j2re14101\lib

--------------------------- The LIBPATH value:

.;C:\Archivos de programa\Java\j2re14101\lib

-------------------------------------------------------
Thanks in advance,
0
rcastan
Asked:
rcastan
  • 5
  • 3
  • 2
1 Solution
 
CEHJCommented:
Your copying method must catch the IOException thrown by various parts of the code. You main methos is also not defined properly:


   public static void main(String args[]) {
       int = fileCopy();
   }
   
   public int fileCopy() {
     try {
       BufferedReader entrada = new BufferedReader(new FileReader("texto.txt"));
       PrintWriter salida = new PrintWriter(new FileWriter("tocoto.txt"), true);
       String line;
       while((line = entrada.readLine()) != null) {
           salida.println(line);
       }
       entrada.close();
       salida.close();
       return 1;
}
catch (IOException e) {
  return -1;
}
   }
} // -- end class prueba
0
 
CEHJCommented:
>>int = fileCopy();

should be

int i = fileCopy();

sorry
0
 
Ajay-SinghCommented:
Try this:-

public static void main(String args[]) {
      int = fileCopy();
}
 
  public int fileCopy() {
    BufferedReader entrada = null;
    PrintWriter salida = null;
    try {
      entrada = new BufferedReader(new FileReader("texto.txt"));
      salida = new PrintWriter(new FileWriter("tocoto.txt"), true);
      String line;
      while((line = entrada.readLine()) != null) {
          salida.println(line);
      }
      return 1;
   }
   catch (IOException e) {
      return -1;
   }finally {
       try {
       if(entrada!=null)
          entrada.close();
       if(salida!=null)
          salida.close();
       }catch(Exception ex) {
              ex.printStackTrace();
       }
   }
  }
} // -- end class prueba
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
rcastanAuthor Commented:
-----------------------------CEHJ:
It just worked fine. Only two comments:
- I had to declare the fileCopy method as "static"
(non-static method fileCopy() cannot be referenced from a static context-main()-)
- the i variable is defined as int in the code I sent:
public void main() {
  int i;
  i = fileCopy();
}

----------------------------Ajay-Singh:
The problem persists in the code you sent me:

----compilation results:
prueba2.java [18:1] cannot resolve symbol
symbol  : class BufferedReader
location: class prueba2
   BufferedReader entrada = null;
   ^
prueba2.java [19:1] cannot resolve symbol
symbol  : class PrintWriter
location: class prueba2
   PrintWriter salida = null;
   ^
prueba2.java [21:1] cannot resolve symbol
symbol  : class BufferedReader
location: class prueba2
     entrada = new BufferedReader(new FileReader("texto.txt"));
                   ^
prueba2.java [21:1] cannot resolve symbol
symbol  : class FileReader
location: class prueba2
     entrada = new BufferedReader(new FileReader("texto.txt"));
                                      ^
prueba2.java [22:1] cannot resolve symbol
symbol  : class PrintWriter
location: class prueba2
     salida = new PrintWriter(new FileWriter("tocoto.txt"), true);
                  ^
prueba2.java [22:1] cannot resolve symbol
symbol  : class FileWriter
location: class prueba2
     salida = new PrintWriter(new FileWriter("tocoto.txt"), true);
                                  ^
prueba2.java [29:1] cannot resolve symbol
symbol  : class IOException
location: class prueba2
  catch (IOException e) {
         ^
7 errors
------------End compilation results.
Is it maybe due to declaring the variables "entrada" and "salida" outside de "try" container?

Thank you both a lot for your time (and knowledge!)
Regards...
0
 
CEHJCommented:
>>I had to declare the fileCopy method as "static"

That's true if you want to call it without first creating an instance of prueba, yes.

>>the i variable is defined as int in the code I sent:

The code i posted is functionally identical to yours, just shorter to write.
0
 
Ajay-SinghCommented:
Adding

import java.io.*;

Should solve all these compilation problems.
0
 
rcastanAuthor Commented:
Again, thanks a lot for your comments.
The sample was compilated succesfully and works. I'm reading now the book "JAVA 2: Fundamentals (Horstmann & Cornell)" trying to improve my skills programming in Java.
Anyway, I've recovered my inner-peace with my "super-application ;-) " compiled and running ... and this is thanks to you.

NOTE.-
Ajay-Singh: In the first post I sent the code and the compilation results. As you can see, it included the "import java.io.*;" statement and in the compilation results the needed classes appear to be loaded.. I don't understand your recommendation.

Regards,
0
 
CEHJCommented:
At *some* point, the compiler could not find BufferedReader, etc. That's why Ajay-Singh recommended importing io.

A points split would be nice then rcastan!
0
 
rcastanAuthor Commented:
Please, can you tell me what shall I do at this point to close properly the question?
Thanks in advance.
Regards
0
 
CEHJCommented:
Sure - ask CS (http://www.experts-exchange.com/Community_Support/) to do a points split for you.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now