Solved

Writing to a file

Posted on 2004-04-10
10
527 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 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java Options in Zimbra Server 2 50
Why my table column Id is not passed to java object? 4 38
spring jars download 1 26
Tomcat: Unable to run tomcat service. 2 18
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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…
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.

816 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now