Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Writing to a file

Posted on 2004-04-10
10
Medium Priority
?
542 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
  • 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 2000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
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.
Suggested Courses

824 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