10 Replies Latest reply: Aug 30, 2008 3:27 PM by 843785 RSS

    Writing to a File

    800313
      Ok first of all i was generating strings, storing them in a string array and then sending the array to my writefile method to get written to a file. This worked great untill my first method generated so many strings that that the jvm ran out of heap space.

      So instead of storing each string in an array i want to print it to the file each time, except im having some diffculty, any help would be apprieciated,
           public static void writefile(String myString, int count,double totalcount){
      
                     try {     
                          FileWriter output = new FileWriter(Filename);
                          PrintWriter mypw = new PrintWriter(output);
                          if(count <1){
                               output.write("******Hex Word List******* ");
                          }else if(count == totalcount({
                                              output.close();
                                      }else{
                               output.write("\n"+myString);
                          }
                     } catch(IOException e) {
                               System.out.println("Cannot save " + Filename);
                               System.out.close();
                               System.exit(0);
                     }//end try 
          }//end of write file
      First i tried the above code and this didnt print anything out to my file, I then trying closing the Filewriter after each string was written but this only wrote the very last string in.

      Calypso
        • 1. Re: Writing to a File
          843785
                                   output.write("\n"+myString);
          You should be using mypw.println(...)
          also see Writer.flush()
          • 2. Re: Writing to a File
            800313
            Just figured out one method of doing:-
            FileWriter output = new FileWriter(Filename, true);
            all bytes of data will now be written to the end of the file instead of the beginning

            both output.write() and mypw.println() work, whats the difference??

            Thanks
            • 3. Re: Writing to a File
              843785
              both output.write() and mypw.println() work, whats the difference??
              RTFM

              before you go trying to use things, you should figure out what they are doing

              http://java.sun.com/javase/6/docs/api/
              • 4. Re: Writing to a File
                843785
                i think all your problem is just in the flush;

                you have to flush after writing as it forces any buffered output bytes to be written out.

                So i think your method should look like that


                public static void writefile(String myString, int count,double totalcount){

                               try {     
                                    FileWriter output = new FileWriter(filename);
                                    PrintWriter mypw = new PrintWriter(output);
                                    if(count <1){
                                         output.write("******Hex Word List******* ");
                                         output.flush();
                                    }else if(count == totalcount){
                output.close();
                }else{
                                         output.write("\n"+myString);
                                         output.flush();
                                    }
                               } catch(IOException e) {
                                         System.out.println("Cannot save " + filename);
                                         System.out.close();
                                         System.exit(0);
                               }//end try
                }//end of write file

                Edited by: MariamMohamed on Aug 29, 2008 12:01 PM
                • 5. Re: Writing to a File
                  800313
                  Thanks guys managed to get it working. How can i work out how many bytes of dating i will be writing

                  For example say I want to write 1,000,000 strings of 10 chars long, how many bytes will this be?

                  Calypso
                  • 6. Re: Writing to a File
                    843785
                    calypso wrote:
                    Thanks guys managed to get it working. How can i work out how many bytes of dating i will be writing

                    For example say I want to write 1,000,000 strings of 10 chars long, how many bytes will this be?

                    Calypso
                    In the time it took to post that you could have whipped up a little test program to give you the answer.

                    By the way, it depends on your encoding.
                    • 7. Re: Writing to a File
                      800313
                           public static void writefile(String myString){
                                     try {     
                                          FileWriter output = new FileWriter(Filename, true);
                                          PrintWriter mypw = new PrintWriter(output);
                                               output.write("\n"+myString);
                                               output.flush();
                                               output.close();
                                     } catch(IOException e) {
                                               System.out.println("Cannot save " + Filename);
                                               System.out.close();
                                               System.exit(0);
                                     }//end try 
                          }//end of write file
                      1. After doing some timed tests this was the most efficient code i could come up with, can anyone see any way to impove the efficeincy even more?

                      2. DrLaszloJamf I tried reading this tuturial as i have never really used bytes before - http://java.sun.com/docs/books/tutorial/i18n/text/string.html
                      but i still dont really understand how to determine how many bytes a string is.

                      i used the myString.getBytes() whihc return a byte array, but when i try and print it out it just seems to print out a reference to memory ie [B@3e25a5

                      Thanks in advance for any help

                      Calypso
                      • 8. Re: Writing to a File
                        843785
                                            PrintWriter mypw = new PrintWriter(output);
                        You never even use this, so clearly your code could be more optimal if it didn't even instantiate it.
                        • 9. Re: Writing to a File
                          800313
                               public static void writefile(String myString){
                                         try {     
                                              FileWriter output = new FileWriter(Filename, true);
                                              PrintWriter mypw = new PrintWriter(output);
                                                   mypw.println(myString);
                                                   mypw.flush();
                                                   mypw.close();
                                                   //output.close();
                                         } catch(IOException e) {
                                                   System.out.println("Cannot save " + Filename);
                                                   System.out.close();
                                                   System.exit(0);
                                         }//end try 
                              }//end of write file
                          I did use the above as you suggested but im afraid it came out slightly slower than the code that i posted previously, it was only by a few seconds slower, but a few seconds will make a fair bit of difference as I will prob have to leave this program to run for maybe a couple of days.

                          Calypso
                          • 10. Re: Writing to a File
                            843785
                            also, nothing to do with "optimization", but it is probably safer to close your streams in a finally block.