This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hi I am new to java. What is the efficient way of writing data into *.txt file using java I need to dump data from one of the oracle table to *.txt file using java. Right now I am using File outputFile = new File(fileName); PrintWriter out =new PrintWriter(new BufferedWriter (new FileWriter(outputFile))); using out.write method to write the data into text file.
Gurus please let me know if there is any other way to acheive same result much faster. Because right now it takes about 30 minutes to create file for about 300,000 records.
If you're just using out.write(), then you don't need the PrintWriter; but that's probably not the issue.
Usually the major performance issues in this kind of code come from the unintentional creation of large numbers of little Strings. We'd have to see some of the actual writing code to understand where the problem might lie there.
There's also the simple issue that it might take 30 minutes to store that much information on your system -- if the disk is a remote share of some kind, for example.
OK, well: each "trim()" call creates a String, and each "+" creates a String and a StringBuilder. Also, flush() is a relatively expensive call. Replicating "trim()" without creating a String would be messy, but you might see a noticeable difference just by doing this:
If that doesn't work, then as an experiment, see how long it takes just to iterate through the ResultSet, without writing anything:
I suspect you may find that it takes just as long - which would indicate that the Java code isn't the problem; it's the connection to the database. If that's the case, then you'll have to look elsewhere for improvements. Maybe the SQL query can be written more efficently, or maybe you can relocate the code to run on the same machine as the DB (assuming it's not doing that already). Or maybe there are advanced DB tuning options. Anyway, find out where the problem really is before you spend too much time trying to fix it. [ January 22, 2007: Message edited by: Jim Yingst ]
"I'm not back." - Bill Harding, Twister
Joined: Apr 01, 2004
Thank you so much for your reply. I am going to change the code to to see if i get any better performance. But in general for a query about million rows I get result set back by 12 minutes. Then it takes another 50 to 60 minutes to dump the data in a file.