?
Solved

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

Posted on 2003-03-30
5
Medium Priority
?
370 Views
Last Modified: 2012-08-13
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
Comment
Question by:kojo12
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 9

Accepted Solution

by:
yongsing earned 180 total points
ID: 8235904
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
 
LVL 7

Expert Comment

by:enzogoy
ID: 8235928
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
 

Expert Comment

by:CleanupPing
ID: 9058383
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
 
LVL 15

Expert Comment

by:jimmack
ID: 9735706
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month10 days, 11 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question