Solved

Writing to a file

Posted on 2004-04-10
10
535 Views
Last Modified: 2010-03-31
I need to read in a file, and then create a file which includes the first file content.  For example i read file A which contains

abc
def

then i need to read in, and then create a file which has the format:

1: abc
2: def

any idea?
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
  • 5
  • 5
10 Comments
 
LVL 92

Expert Comment

by:objects
ID: 10796070
BufferedReader in = new BufferedReader(new FileReader("in.txt"));
PrintWriter out = new PrintWriter(new FileReader("out.txt"));
String line = null;
int lineno = 1;
while (null!=(line=in.readLine()))
{
   out.println(lineno+": "+line);
   lineno++;
}
in.close();
out.close();

0
 

Author Comment

by:jtcy
ID: 10796272
I have this method in class Parser,

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

    }

then in driver class, i do:

parser.writeFile(inFile);

the Listing file did create but is still blank~ :~(
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 10796285
that won't work cause you don't read the next line in your loop.

should be more like:

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

    }
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:jtcy
ID: 10796296
I changed it, still doesnt work. 0kb file produced~
0
 

Author Comment

by:jtcy
ID: 10796305
FileWriter writer = new FileWriter("LISTING.txt");
          PrintWriter out = new PrintWriter(writer);
          
          BufferedReader inFile = new BufferedReader(in);
          String line = null;
        System.out.println(inFile.readLine()); // I do a test here

and it seems like the file read in is null. how can that be. i am very sure the file is not null. Could it be perhaps we cant pass Filereader object as parameter?
0
 
LVL 92

Expert Comment

by:objects
ID: 10796308
SOunds like either the file you are reading is empty, or you have already read all it's contents.
0
 
LVL 92

Expert Comment

by:objects
ID: 10796313
Make sure you haven't already been reading from the FileReader you are passing in.
0
 

Author Comment

by:jtcy
ID: 10796325
>> Make sure you haven't already been reading from the FileReader you are passing in.


cool. solved it.
0
 
LVL 92

Expert Comment

by:objects
ID: 10796344
0
 

Author Comment

by:jtcy
ID: 10796346
hey, u're my senior LOL
0

Featured Post

Independent Software Vendors: 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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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…
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 video teaches viewers about errors in exception handling.
Suggested Courses

623 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