Solved

FileReader & PrintWriter

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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jsp error 6 43
project copy paste issue 1 62
Java array 21 79
Java Inharitance subclass/child extends, reference variable from base/parent class 9 29
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

735 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