Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Opening and closing multiple files in a while loop.

PaulieFeb 7 2014 — edited Feb 12 2014

Hi all,

I'm relatively new to Java programming, but I believe that I have a bit of a clue

and I have Googled and this is not homework.

I'm going through a file (MySQL dump) and I wish to take the SQL and create

individual files for each table (as opposed to what I do now, which is just

run through the whole thing).

<code>

  BufferedReader myDumpfileReader = new BufferedReader(new FileReader(tfDumpfile.getText()));

  BufferedWriter myDumpfileWriter = new BufferedWriter(new FileWriter("/users/linehanp/mydb/bin/tgac_dump/Vanilla.sql"));

while((dumpFileLine = myDumpfileReader.readLine()) != null)
{

      DoStuff();

      MyDumfileWriter.write("Whatever");

  }

myDumpfileReader.close();

myDumpfileWriter.close();

</code>

Now, this all works fine - but what I want to do is create a separate .sql file for each table.

However, I can't figure out the best way to do this - how do I create a new FileWriter (or

instance of whatever class is required) as I'm going through the while loop? That or

pointing the same one to different files as I go through the loop.

It can be assumed that the file may exceed RAM - I want to do it line by line rather than slurping

the entire file as a string and "cheating" that way.

There appear to be many (a bewildering amount of) I/O options and I'm just unsure.

I would appreciate any help/advice/URLs/suggestions - whatever.

TIA and rgs,

Paul...

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 12 2014
Added on Feb 7 2014
3 comments
1,817 views