Solved

FileReader & PrintWriter

Posted on 2004-04-10
5
1,236 Views
Last Modified: 2011-10-03
Can a FileReader object get written twice? I just create a file using PrintWriter with a FileReader object as its argument. Result of that is a file created. Then i need to write something to that file which just created. So i create a printWriter object again with that FileReader object. But that doesnt work. Can someone help?

public void writeFile(FileReader in) throws java.io.IOException
    {
          
          PrintWriter out = new PrintWriter(writer);
          
          BufferedReader inFile = new BufferedReader(in);
          String line =null;
         
         int j = 1;
         while ((line = inFile.readLine())!= null)
          {
                 out.println(j+ " :   " + line);
                 j++;
           }
           out.println("");
           out.println("");
          
          inFile.close();
            out.close();

    }

so I first call
writeFile(inFile);
then i do:

PrintWriter err = new PrintWriter(parser.writer);
            // add any error messages to the listing file...

                for (int i=0; i<parser.errors.size(); i++)
                 {
                       String k = (String)parser.errors.get(i);
                       err.println(k);
                 }
               
                err.close();

the problem is nothing being added to the file which just created.
0
Comment
Question by:jtcy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 200 total points
ID: 10796583
The easiest way is to siimply append the error messages:

PrintWriter out = new PrintWriter(new FileWriter("yourFile.log", true)); // open for append
out.println("Extra info");
out.close();

0
 
LVL 92

Assisted Solution

by:objects
objects earned 300 total points
ID: 10799178
Have your writeFile method return your PrintWriter so you can continue to write to the file:

public PrintWriter writeFile(FileReader in) throws java.io.IOException
    {
         
         PrintWriter out = new PrintWriter(writer);
         
         BufferedReader inFile = new BufferedReader(in);
         String line =null;
         
         int j = 1;
         while ((line = inFile.readLine())!= null)
          {
               out.println(j+ " :   " + line);
               j++;
          }
          out.println("");
          out.println("");
         
         inFile.close();
         return out;

    }
0
 
LVL 92

Assisted Solution

by:objects
objects earned 300 total points
ID: 10799184
You'd then write your errors using:

PrintWriter err = writeFile(inFile);
// add any error messages to the listing file...

for (int i=0; i<parser.errors.size(); i++)
{
        String k = (String)parser.errors.get(i);
        err.println(k);
}
             
err.close();
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10812283
8-)
0
 
LVL 92

Expert Comment

by:objects
ID: 10817893
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Running JavaFX on JDeveloper 12C 1 116
glassfish admin console 1 53
What browser will run Java? 7 181
Bitwise and to sum elements 2 54
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question