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

x
?
Solved

Writing to a file

Posted on 2004-04-10
10
Medium Priority
?
539 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 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses

721 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