This week's book giveaway is in the Agile forum.
We're giving away four copies of Fixing your Scrum and have Ryan Ripley & Todd Miller on-line!
See this thread for details.
Win a copy of Fixing your Scrum this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

How to output result set into individual text file?

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am able to return result set from query into text file but i want it once it query it auto save into a new file instead of creating all in one text file.

It have 5 return result but its all save inside 1 text file which i want it to be in 5 text file.

Here is my code.


 
Sheriff
Posts: 16564
272
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then you'd need to move lines 16 and 18 inside the loop and update the count variable as you go, right? Your current code creates only one BufferedWriter so that's why everything is getting written to just that one file.

Be sure to adjust where you're closing your I/O resources as well.
 
kelvin Seow
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Junilu Lacar,

Do you mean like this?

 
kelvin Seow
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,

I realize my mistake my count was inside that why it could generate 5 files out. Thanks for the help Juilan

by the way since my folder have 5 files how do i do a loop to look through that folder for all the 5 files?

 
Junilu Lacar
Sheriff
Posts: 16564
272
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kelvin Seow wrote:Do you mean like this?


Close, but there's still a mistake there. At the start of each iteration of the loop, you set count to 0. That will make your code always output to a SQLRetrieve0.txt file, no matter how many iterations you have.

I suggest you look through the API documentation for java.nio.file.Files and java.nio.file.DirectoryStream. There are examples in the latter that will answer your other question.

kelvin Seow wrote:Thanks for the help Juilan


You're welcome, but it's Junilu, not Juilan
 
kelvin Seow
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Junilu,

Sorry for the mistake. Oh 1 more question how do i loop through a folder which contains the 5 file?
 
Junilu Lacar
Sheriff
Posts: 16564
272
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please read the suggestion in my previous response carefully. Thanks.
 
You firghten me terribly. I would like to go home now. Here, take this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic