1

I have a program at the moment that links multiple SQL statements together in the form of a mapping. I then write this data to a file using FileWriter.

I was wondering if it is better to store the information from my SQL statements in a string buffer and then write directly to file, or, would it be best to write to a file line by line when necessary ?

I actually already write to the file using one string buffer but encountered a problem, the program would crash once the string buffer collected a value exceeding around 125000 characters.

Nathan
  • 266
  • 4
  • 16
  • http://stackoverflow.com/questions/4576222/fastest-way-to-write-to-file – Mustafa Genç Feb 13 '14 at 12:45
  • What is the question exactly? Where do these SQL statements come from that you can exceed 125k chars of them? – fge Feb 13 '14 at 12:48
  • The SQL statements map together - So I call select * from a specific database, then for the value at each line of the result set for a specific column (which is read from a JTable) I then call a different SQL statement after each line is read, using the value obtained from the desired column to retrieve the records required – Nathan Feb 13 '14 at 12:56
  • The answer is yes ... and no. Probably for up to about a half meg you're better off using a single write, in terms of overall performance. – Hot Licks Feb 13 '14 at 13:00
  • If you're crashing at 125K one suspects the crash is due to some cause other than the size of the string buffer. – Hot Licks Feb 13 '14 at 13:03

1 Answers1

0

In my opinion I always like to create the string I'm going to save in a StringBuffer first and then store it in a file so that I only have to deserialize it once. Which usually leads to performance gains for me.

Another benefit from that is there are less points that there can be a critical error and therefore a would be a fail point. Whereas with the StringBuffer there is usually only one point of failure.

dmcqu314
  • 875
  • 11
  • 29
  • I think what I've decided upon is performing checks against the character count within the StringBuffer, if it exceeds a certain threshold, save it to a file, then clear it, and if I wish to write it again, read back from a file into an array list, update the array list, and then write it back -- idea modified from http://stackoverflow.com/questions/289965/inserting-text-into-an-existing-file-via-java – Nathan Feb 13 '14 at 12:59
  • 2
    @Nathan - You shouldn't read the file back. You should open it for "append" and add more data to the end. – Hot Licks Feb 13 '14 at 13:03