Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 387
  • Last Modified:

how to save to a multi-dimensional array to text file

i have a method to load a text file. other methods then call this method to load the contents of the text file into an array.

code:

static String[][]loadFile()
         {
            JOptionPane.showMessageDialog(null, "TESTING: Inside 'Load File' method");

            String flights[][] = new String[21][5];

            try
               {
                  FileReader file = new FileReader("Assign.txt");
                  BufferedReader buff = new BufferedReader(file);

                  for(int row = 0; row < flights.length; row++)
                     {
                        for(int column = 0; column < 5; column++)
                           {
                              flights[row][column] = buff.readLine();
                           }
                     }
                  buff.close();
               }

            catch(IOException e)
               {
                  System.out.println("Error reading file"+e.toString());
               }

            JOptionPane.showMessageDialog(null, "TESTING: End of 'Load File' method");
             
            return flights;

         }

anyway, as you can see i'm using a file reader to read the file into a buffer and then putting each line the buffer takes into a multi-dimensional array.

what i need to know is how to save to a multi-dimensional array to text file now and overwrite the current contents.

any help is appreciated
0
kojo12
Asked:
kojo12
1 Solution
 
yongsingCommented:
try {
  BufferedWriter writer = new BufferedWriter(new FileWriter("Assign.txt", false));
  for(int row = 0; row < flights.length; row++) {
    for(int column = 0; column < 5; column++) {
      String line = flights[row][column];
      writer.write(line, 0, line.length());
      writer.newLine();
    }
  }
  writer.close();
} catch(IOException e) {
  System.out.println("Error reading file"+e.toString());
}
0
 
enzogoyCommented:
You just using the FileReader just for reading the file, if you want to save that file into a textfile.  You have to use the PrintWriter, which somehow similar with the File Reader but do the opposite job.

More about the PrintWriter, you may visit the www.sun.com and search for the API.  

Hope it could help you.

cheers

0
 
CleanupPingCommented:
kojo12:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
jimmackCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept yongsing's comment as answer.

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jimmack
EE Cleanup Volunteer
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now