Link to home
Start Free TrialLog in
Avatar of ellandrd
ellandrdFlag for Ireland

asked on

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]
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> 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 () ;
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() ) ) ;



Avatar of ellandrd

ASKER

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
That's ok - I've asked Venabili to look into it and decide.
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?
Let it be as it is.
So I take it you had a problem then...
No, its fine. No problem. Actually after I posted my second comment I realized you accepted it.
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....
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
No, its perfectly fine :) he deserved all points for this.