Solved

FileReader & PrintWriter

Posted on 2004-04-10
5
1,221 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
  • 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now