Solved

Writing to a file

Posted on 2004-04-10
10
525 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

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

Author Comment

by:jtcy
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 92

Expert Comment

by:objects
Comment Utility
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
Comment Utility
Make sure you haven't already been reading from the FileReader you are passing in.
0
 

Author Comment

by:jtcy
Comment Utility
>> 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
Comment Utility
0
 

Author Comment

by:jtcy
Comment Utility
hey, u're my senior LOL
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

771 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

16 Experts available now in Live!

Get 1:1 Help Now