Solved

FileReader & PrintWriter

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

617 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