write to file

ive got 2 strings:

String str1 = "bah bah bah bah bah 12";
String str2 = "bah bah again bah bah again 01234567";

these are created by read the contents of a file.

im trying to write them back a another file and its kinda working only it sonlyw rites the last 2 lines form the file it read.

so the file im reading is:

"age","phone number"
"Jane","New York","Jan",38,2221234567
"Tom","New York","Feb",45,2221234568
"David","New York","Mar","Actual",24,2221234569

im wanting to output it in another file in this format:

"Jane","New York","Jan","age",38
"Jane","New York","Jan","phone number",2221234567
"Tom","New York","Feb","age",45
"Tom","New York","Feb","phone number",2221234568
"David","New York","Mar","age",24
"David","New York","Mar","phone number",2221234569

Ive got the format working now, but as i said above it only writes the last two lines to the file...

[code]
public class ColumnFormatting {
    private BufferedReader br = null ;
    private BufferedWriter bw = null ;
    private FileWriter fw = null ;
    private String str, str2, str3 = null ;
    private String[] headers, str1 = null ;
   
    public ColumnFormatting() {
        readFile() ;
        writeFile() ;
    }
   
    public void readFile() {
        try {
            br = new BufferedReader( new FileReader( "etc\\columnformatting.txt" ) ) ;
            str = br.readLine() ;
            headers = str.split( "," ) ;
            while ( ( str = br.readLine() ) != null ) {
                process( headers, str ) ;
            }
            br.close() ;
        } catch ( IOException e ) {
        }
    }
   
    private void process( String[] headers, String str ) {
        str1 = str.split( "," ) ;
        str2 = str1[ 0 ] + "," + str1[ 1 ] + "," + str1[ 2 ] + "," + headers[ 0 ] + "," + str1[ str1.length -2 ] ;
        str3 = str1[ 0 ] + "," + str1[ 1 ] + "," + str1[ 2 ] + "," + headers[ 1 ] + "," + str1[ str1.length -1 ] ;
    }
   
    public void writeFile() {
        try {
            fw = new FileWriter( new File( "etc\\columnsformatted.txt" ) ) ;
            bw = new BufferedWriter( fw ) ;
            bw.write( str2, 0, (str2.length() ) ) ;
            bw.newLine() ;
            bw.write( str3, 0, (str3.length() ) ) ;
            bw.newLine() ;
            bw.close() ;
            fw.close() ;
        } catch ( IOException e ) {
        }
    }

    public static void main( String[] args ) {
        new ColumnFormatting() ;
    }
}
[/code]
LVL 16
ellandrdAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

objectsCommented:
you need to do the write inside your loop

    public void readFile() {
        try {
            fw = new FileWriter( new File( "etc\\columnsformatted.txt" ) ) ;
            bw = new BufferedWriter( fw ) ;
            br = new BufferedReader( new FileReader( "etc\\columnformatting.txt" ) ) ;
            str = br.readLine() ;
            headers = str.split( "," ) ;
            while ( ( str = br.readLine() ) != null ) {
                process( bw, headers, str ) ;
            }
            br.close() ;
             bw.close() ;
       } catch ( IOException e ) {
        }
    }
   
    private void process(BufferedWriter bw,  String[] headers, String str ) {
        str1 = str.split( "," ) ;
        str2 = str1[ 0 ] + "," + str1[ 1 ] + "," + str1[ 2 ] + "," + headers[ 0 ] + "," + str1[ str1.length -2 ] ;
        str3 = str1[ 0 ] + "," + str1[ 1 ] + "," + str1[ 2 ] + "," + headers[ 1 ] + "," + str1[ str1.length -1 ] ;
            bw.write( str2, 0, (str2.length() ) ) ;
            bw.newLine() ;
            bw.write( str3, 0, (str3.length() ) ) ;
            bw.newLine() ;
    }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mayank SAssociate Director - Product EngineeringCommented:
>> str2 = str1[ 0 ] + "," + str1[ 1 ] + "," + str1[ 2 ] + "," + headers[ 0 ] + "," + str1[ str1.length -2 ] ;

BTW, if the file grows large, you ought to think of performance :) use a StringBuffer or a StringBuilder (Java 5):

String buffer = new StringBuffer ( str1[0] ) ;
buffer.append ( ", " ) ;
buffer.append ( str[1] ) ;
buffer.append ( ", " ) ;
buffer.append ( str[2] ) ;
buffer.append ( ", " ) ;
buffer.append ( headers[0] ) ;
buffer.append ( ", " ) ;
buffer.append (  str1[ str1.length -2 ] ) ;
String str2 = buffer.toString () ;
0
Mayank SAssociate Director - Product EngineeringCommented:
I hope that from the change in code done by objects, you are able to realize what the problem actually was. You were calling process () in a loop:

>> while ( ( str = br.readLine() ) != null ) {
>> process( headers, str ) ;

And in process (), each time you were modifying str1, str2, str3 without writing them to the file, hence not saving their old values. And finally writing only 2 in the writeFile () method:

>> bw.write( str2, 0, (str2.length() ) ) ;
>> bw.write( str3, 0, (str3.length() ) ) ;



0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

ellandrdAuthor Commented:
oops sorry mayankeagle,

didnt see your post when i clicked accept.  it wasnt there!  That advice is useful and if i had seen it i would have included you in on the points... but maybe next time you'll will faster... ;-0

ellandrd
0
Mayank SAssociate Director - Product EngineeringCommented:
That's ok - I've asked Venabili to look into it and decide.
0
ellandrdAuthor Commented:
Eh? look into it and decide what? Whats wrong now?

Your comments where not posted when I accepted objects solution, hence why you where not included in the points.

Yes I do value your advice and it was taken on board but i dont understand why you are bringing in a page editor?
0
Mayank SAssociate Director - Product EngineeringCommented:
Let it be as it is.
0
ellandrdAuthor Commented:
So I take it you had a problem then...
0
Mayank SAssociate Director - Product EngineeringCommented:
No, its fine. No problem. Actually after I posted my second comment I realized you accepted it.
0
Mayank SAssociate Director - Product EngineeringCommented:
I actually thought you meant you wanted to put my first comment as an assist but I guess that's not the case - you were probably accepting this even before that one was posted - so its fine. And I hope we don't fight here like you did with somebody else on your other Q ;-) he he....
0
ellandrdAuthor Commented:
mayankeagle,

What happened in the other Q was abit childish by him, getting up and leaving - i think i read and took his meaning of his comment the wrong way, hence making me think he was having a go at me (a.k.a attacking me) as all i done was ask a simple question about setTitle()...

In this thread, I had already accepted the solution from objects, when I saw your first comment, hence why i said: "oops sorry mayankeagle..."

If i had seening your comments before i accepted objects solution, you would have been included in the points, like i have already said above.

The advice you continued to provide is and was helpful and I took it on broad.

If you want I can reopen this Q and split the points if objects has no objections to this?  and on saying that, he's in Austrailia so he's in bed now (if not still helping others) so it'll be tomz before we know, but i'm willing to give you some points if you felt to deserved some...

Ellandrd

p.s im not here to fall out with people - yes i do ask some really st*p*d questions in the Java TA, but up to a few months ago I didnt know Java and since then ive be learning it all off you guys.  

And yes, its been a few months now so people think, hey he should know that by now... well there are a few times im unsure about things so i like to ask as the javadocs dont have all the answers....

anyways lets get back to work...

ellandrd
0
Mayank SAssociate Director - Product EngineeringCommented:
No, its perfectly fine :) he deserved all points for this.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.