Emma Rossignoli
Emma Rossignoli

Reputation: 945

Save JTable content to TextFile and read it

I have a problem with my jTable. I read a lot of stuff on the net but I still can't solve my problem. When I click on jButton1, this procedure is called (Saves to text file the jtable)

    public void SaveMyTable()throws Exception
{
  BufferedWriter bfw = new BufferedWriter(new FileWriter("C:\\emma\\mystuff\\database.txt"));
  for(int i = 0 ; i < jTable1.getColumnCount() ; i++)
  {
    bfw.write(jTable1.getColumnName(i));
    bfw.write("\t");
  }

  for (int i = 0 ; i < jTable1.getRowCount(); i++)
  {
    bfw.newLine();
    for(int j = 0 ; j < jTable1.getColumnCount();j++)
    {
      bfw.write((String)(jTable1.getValueAt(i,j)));
      bfw.write("\t");;
    }
  }
  bfw.close();
}

With this code, I save the datas in this way:

n   Col1    Col2    Col3
1   a       b       c

I'd like that, when the program runs, it loads this file from the path above and soI thought to use the BufferedReader method. By the way, I don't know how to do this. Do you have any ideas? :) Here you can see my code, I've made only a part of it because I don't know how to continue.

public void LoadMyTable()throws Exception
{
  BufferedReader br = new BufferedReader(new FileReader("C:\\addressbook\\databese.txt"));
    try {
        StringBuilder sb = new StringBuilder();
        String line = br.readLine();
   // code that loads the datas in my jTable
} finally {
    br.close();
}

Upvotes: 0

Views: 2414

Answers (2)

silence
silence

Reputation: 1

This is the method I used in order to keep the columns in order. Its a bit messy and inefficient but the character constant is 20.

BufferedWriter bfw = new BufferedWriter(new FileWriter(
                    "Data.txt"));

            for (int i = 0; i < table.getColumnCount(); i++) {//first loop is used for titles of each column

                String name = String.valueOf(table.getColumnName(i));

                if (name.length() > 20) {//20 (characters long) is the constant I chose to make each value
                    name = name.substring(0, 20);
                } else if (name.length() == 20) {

                } else {
                    String spaces = "";
                    int diff = 20 - name.length();
                    while (diff > 0) {
                        spaces = spaces + " ";
                        diff--;
                    }
                    name = name.concat(spaces);
                }

                bfw.write(name);
                bfw.write("\t");
            }

            for (int i = 0; i < table.getRowCount(); i++) {//for all the data in the Jtable excluding column headers
                bfw.newLine();
                for (int j = 0; j < table.getColumnCount(); j++) {

                    if (table.getValueAt(i, j) == null) {
                        bfw.write("                    ");
                        bfw.write("\t");
                    }

                    else {

                        String name = String.valueOf((table
                                .getValueAt(i, j)));

                        if (name.contains("(")) {
                            name = name.substring(0, name.indexOf("("));
                        }

                        if (name.length() > 20) {
                            name = name.substring(0, 20);
                        } else if (name.length() == 20) {

                        } else {
                            String spaces = "";
                            int diff = 20 - name.length();
                            while (diff > 0) {
                                spaces = spaces + " ";
                                diff--;
                            }
                            name = name.concat(spaces);
                        }

                        bfw.write(name);
                        bfw.write("\t");
                    }
                }
            }

Upvotes: 0

nachokk
nachokk

Reputation: 14413

You need something like this.

              BufferedReader br = null;
            try {
                br = new BufferedReader(new FileReader(path));
                String line = null;


            while ((line = br.readLine()) != null) {
                //here you have to load your table 

            }

        } catch (IOException e) {
            //manage your exceptions
        } finally {
            try {
                if (br != null){
                               br.close(); // close open streams resources
                            }
            } catch (IOException ex) {
                //manage your exceptions
            }
        }

readLine() : Reads a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.

May this help you

Upvotes: 2

Related Questions