problem in FileWriter

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shana07
    Contributor
    • Jan 2007
    • 280

    problem in FileWriter

    Hi, I am trying to write my output into a text file using FileWriter.
    I have about a thousand entries in my ArrayLists.
    So what happen is it just prints out the last entry. I've gone through couple examples from web about FileWriter, it involves with looping for. Could anyone please teach me how to get it printed all entries in line.
    Please help me to take a look at it....really appreciate any advice. Thank you.
    Code:
    myArrayList.add(new Entry(data, number, value));  //If print out myArrayList.size() = 1000.
    try
            {
                // WRITE ENTRY TO FILE 
                output = new BufferedWriter( new FileWriter(FILENAME));     
                output.write(data+"\n");
                output.write(number+"\n");
                output.write(value+"\n");                
                
                output.close();     
                
             } 
              
               catch (IOException e) 
               {
                  System.out.println("There was a problem creating/writing to the temp file");
                  e.printStackTrace();
               }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by shana07
    Hi, I am trying to write my output into a text file using FileWriter.
    I have about a thousand entries in my ArrayLists.
    So what happen is it just prints out the last entry. I've gone through couple examples from web about FileWriter, it involves with looping for. Could anyone please teach me how to get it printed all entries in line.
    Please help me to take a look at it....really appreciate any advice. Thank you.
    Code:
     
    myArrayList.add(new Entry(data, number, value)); //If print out myArrayList.size() = 1000.
    try
    {
    // WRITE ENTRY TO FILE 
    output = new BufferedWriter( new FileWriter(FILENAME)); 
    output.write(data+"\n");
    output.write(number+"\n");
    output.write(value+"\n"); 
     
    output.close(); 
     
    } 
     
    catch (IOException e) 
    {
    System.out.println("There was a problem creating/writing to the temp file");
    e.printStackTrace();
    }
    If you search for one of the Tips posted here (the first one), you will find a way of writing to a file in append mode rather than overwriting the previous contents

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by shana07
      Hi, I am trying to write my output into a text file using FileWriter.
      I have about a thousand entries in my ArrayLists.
      So what happen is it just prints out the last entry. I've gone through couple examples from web about FileWriter, it involves with looping for. Could anyone please teach me how to get it printed all entries in line.
      Please help me to take a look at it....really appreciate any advice. Thank you.
      Code:
      myArrayList.add(new Entry(data, number, value));  //If print out myArrayList.size() = 1000.
      try
              {
                  // WRITE ENTRY TO FILE 
                  output = new BufferedWriter( new FileWriter(FILENAME));     
                  output.write(data+"\n");
                  output.write(number+"\n");
                  output.write(value+"\n");                
                  
                  output.close();     
                  
               } 
                
                 catch (IOException e) 
                 {
                    System.out.println("There was a problem creating/writing to the temp file");
                    e.printStackTrace();
                 }
      As it is now your code opens the same file everytime you add a 'record' to your
      ArrayList. By opening the file for writing, any previous content of the file is erased.
      So effectively the only entry in your file will be the last entry.

      Either open your file in 'append' mode (see the API docs how to do it) or even
      better: open the file once and keep on writing to it while you loop over your
      ArrayList. At the end close the file again. The reason the second alternative is
      better is that opening a file takes a relatively large amount of time. Opening and
      closing it for each entry would slow down the entire process.

      kind regards,

      Jos

      Comment

      • shana07
        Contributor
        • Jan 2007
        • 280

        #4
        Originally posted by r035198x
        If you search for one of the Tips posted here (the first one), you will find a way of writing to a file in append mode rather than overwriting the previous contents
        thanks, how to go the previous tips anyway...

        Comment

        • shana07
          Contributor
          • Jan 2007
          • 280

          #5
          Originally posted by JosAH
          As it is now your code opens the same file everytime you add a 'record' to your
          ArrayList. By opening the file for writing, any previous content of the file is erased.
          So effectively the only entry in your file will be the last entry.

          Either open your file in 'append' mode (see the API docs how to do it) or even
          better: open the file once and keep on writing to it while you loop over your
          ArrayList. At the end close the file again. The reason the second alternative is
          better is that opening a file takes a relatively large amount of time. Opening and
          closing it for each entry would slow down the entire process.

          kind regards,

          Jos
          Thanks for the clear explanation..re ally appreciate it. I think I prefer the second way because later I need to read them again in second execution and compare with current time entry. so I do need a loop here - using myArrayList.siz e()?

          Code:
          myArrayList.add(new Entry(data, number, value));  //If print out myArrayList.size() = 1000.
          try
          {
                      // WRITE ENTRY TO FILE 
          for(int i=0; i<myArrayList.size(); i++)
          {                 
                      output = new BufferedWriter( new FileWriter(FILENAME));     
                      output.write(data+"\n");  //how to put index for these attributes then...?
                      output.write(number+"\n");
                      output.write(value+"\n");                
                      
                      output.flush()
                      output.close();                 
                   } 
            }
                    
                     catch (IOException e) 
                     {
                        System.out.println("There was a problem creating/writing to the temp file");
                        e.printStackTrace();
                     }

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by shana07
            thanks, how to go the previous tips anyway...
            Links have just been provided. You should just open the file once, like Jos said there, though.

            Comment

            • shana07
              Contributor
              • Jan 2007
              • 280

              #7
              by putting true in the line, it appends to the same file. but I noticed the second Entry line and above it's repeated....
              Code:
              output = new BufferedWriter( new FileWriter(FILENAME, true));

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by shana07
                by putting true in the line, it appends to the same file. but I noticed the second Entry line and above it's repeated....
                Code:
                output = new BufferedWriter( new FileWriter(FILENAME, true));
                Didn't really get that. What exactly is repeated? Also did you follow Jos' advice and write to the file only once?

                Comment

                • shana07
                  Contributor
                  • Jan 2007
                  • 280

                  #9
                  Originally posted by r035198x
                  Didn't really get that. What exactly is repeated? Also did you follow Jos' advice and write to the file only once?
                  yes. that's what I did. There are repeated output lines:
                  example output:
                  data10true
                  data21false
                  data21false // repeated twice
                  data32false
                  ....//repeated 3x
                  and on...

                  my codes:
                  Code:
                  myArrayList.add(new Entry(data, number, value));  //If print out myArrayList.size() = 1000.
                  try
                  {
                              // WRITE ENTRY TO FILE 
                       for(int i=0; i<myArrayList.size(); i++)
                  {                 
                              output = new BufferedWriter( new FileWriter(FILENAME, true));     
                              output.write(data+"\n");  
                              output.write(number+"\n");
                              output.write(value+"\n");  
                        } 
                              output.flush()
                              output.close();                 
                    }
                            
                             catch (IOException e) 
                             {
                                System.out.println("There was a problem creating/writing to the temp file");
                                e.printStackTrace();
                             }

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by shana07
                    yes. that's what I did. There are repeated output lines:
                    example output:
                    data10true
                    data21false
                    data21false // repeated twice
                    data32false
                    ....//repeated 3x
                    and on...

                    my codes:
                    Code:
                    myArrayList.add(new Entry(data, number, value)); //If print out myArrayList.size() = 1000.
                    try
                    {
                    // WRITE ENTRY TO FILE 
                    for(int i=0; i<myArrayList.size(); i++)
                    { 
                    output = new BufferedWriter( new FileWriter(FILENAME, true)); 
                    output.write(data+"\n"); 
                    output.write(number+"\n");
                    output.write(value+"\n"); 
                    } 
                    output.flush()
                    output.close(); 
                    }
                     
                    catch (IOException e) 
                    {
                    System.out.println("There was a problem creating/writing to the temp file");
                    e.printStackTrace();
                    }
                    If that's the code you are using then you have not yet followed the advice. You are still opening the file inside the for loop meaning that it will be opened many times.

                    Comment

                    • shana07
                      Contributor
                      • Jan 2007
                      • 280

                      #11
                      Originally posted by r035198x
                      If that's the code you are using then you have not yet followed the advice. You are still opening the file inside the for loop meaning that it will be opened many times.
                      So sorry i did paste wrong one. I open file before looping and that's what I get that repeated output.

                      Code:
                      myArrayList.add(new Entry(data, number, value)); //If print out myArrayList.size() = 1000.
                      try
                      {
                      output = new BufferedWriter( new FileWriter(FILENAME, true)); 
                      
                      // WRITE ENTRY TO FILE 
                      for(int i=0; i<myArrayList.size(); i++)
                      { 
                      output.write(data+"\n"); 
                      output.write(number+"\n");
                      output.write(value+"\n"); 
                      } 
                      output.flush()
                      output.close(); 
                      }
                       
                      catch (IOException e) 
                      {
                      System.out.println("There was a problem creating/writing to the temp file");
                      e.printStackTrace();
                      }

                      Comment

                      • ashokingroups
                        New Member
                        • Feb 2007
                        • 13

                        #12
                        Hi,
                        I'm new member to this site. Please don't feel bad, and my kind request is that please give 2 minutes of your valuable time.

                        I've registered with this site nearly 3 or 4 months before only. I sent some questions also.

                        While registration, I've given my mail id <email removed by ADMIN>

                        But, I couldn't able to get even single reply also. Now my doubt is that whether my question is appearing in the forum or not?

                        Could you please suggest me how to proceed so that I can post my question properly and I could able to get answers also to my mail address for them?
                        please.....
                        Thankyou.
                        Waiting for your reply..... Ashok kumar.

                        Comment

                        • shana07
                          Contributor
                          • Jan 2007
                          • 280

                          #13
                          Originally posted by ashokingroups
                          Hi,
                          I'm new member to this site. Please don't feel bad, and my kind request is that please give 2 minutes of your valuable time.

                          I've registered with this site nearly 3 or 4 months before only. I sent some questions also.

                          While registration, I've given my mail id as: ashokingroups@y ahoo.co.in

                          But, I couldn't able to get even single reply also. Now my doubt is that whether my question is appearing in the forum or not?

                          Could you please suggest me how to proceed so that I can post my question properly and I could able to get answers also to my mail address for them?
                          please.....
                          Thankyou.
                          Waiting for your reply..... Ashok kumar.
                          I think publish email add in TSDN is not permitted. So if you have any questions
                          you have to open discussion topic and someone will answer it there.

                          Comment

                          • r035198x
                            MVP
                            • Sep 2006
                            • 13225

                            #14
                            Originally posted by shana07
                            So sorry i did paste wrong one. I open file before looping and that's what I get that repeated output.

                            Code:
                            myArrayList.add(new Entry(data, number, value)); //If print out myArrayList.size() = 1000.
                            try
                            {
                            output = new BufferedWriter( new FileWriter(FILENAME, true)); 
                             
                            // WRITE ENTRY TO FILE 
                            for(int i=0; i<myArrayList.size(); i++)
                            { 
                            output.write(data+"\n"); 
                            output.write(number+"\n");
                            output.write(value+"\n"); 
                            } 
                            output.flush()
                            output.close(); 
                            }
                             
                            catch (IOException e) 
                            {
                            System.out.println("There was a problem creating/writing to the temp file");
                            e.printStackTrace();
                            }
                            But you are not writing data from the arraylist you should have something like

                            Code:
                             for(int i=0; i<myArrayList.size(); i++) 
                            { 
                            Entry entry = (Entry)myArrayList.get(i);
                            then
                            Code:
                             output.write(entry.getData());

                            Comment

                            • JosAH
                              Recognized Expert MVP
                              • Mar 2007
                              • 11453

                              #15
                              Originally posted by shana07
                              So sorry i did paste wrong one. I open file before looping and that's what I get that repeated output.

                              Code:
                              myArrayList.add(new Entry(data, number, value)); //If print out myArrayList.size() = 1000.
                              try
                              {
                              output = new BufferedWriter( new FileWriter(FILENAME, true)); 
                              
                              // WRITE ENTRY TO FILE 
                              for(int i=0; i<myArrayList.size(); i++)
                              { 
                              output.write(data+"\n"); 
                              output.write(number+"\n");
                              output.write(value+"\n"); 
                              } 
                              output.flush()
                              output.close(); 
                              }
                               
                              catch (IOException e) 
                              {
                              System.out.println("There was a problem creating/writing to the temp file");
                              e.printStackTrace();
                              }
                              What's bothering me is that first line: myArrayList.add ( ... ). You're not trying
                              to write everything to a file from a routine that adds a single element to your
                              list do you? If you'd add a single element again data, number, value will be
                              written for every element in your list, hence the repetitions.

                              kind regards,

                              Jos

                              Comment

                              Working...