Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

FileReader & PrintWriter

Posted on 2004-04-10
5
Medium Priority
?
1,247 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 400 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 600 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 600 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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

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…
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…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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:
Suggested Courses

705 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