将JTable内容保存到文本文件并读取它

时间:2013-06-18 21:48:38

标签: java swing file-io jtable

我的jTable有问题。我在网上看了很多东西,但我仍然无法解决我的问题。当我单击jButton1时,将调用此过程(将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();
}

使用此代码,我以这种方式保存数据:

n   Col1    Col2    Col3
1   a       b       c

我想这样,当程序运行时,它从上面的路径加载这个文件,所以我想使用BufferedReader方法。顺便说一句,我不知道该怎么做。你有什么想法? :)在这里你可以看到我的代码,我只做了一部分,因为我不知道如何继续。

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();
}

2 个答案:

答案 0 :(得分:2)

你需要这样的东西。

              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():读取一行文本。一条线被认为是由换行符('\ n'),回车符('\ r')或回车符后面的任何一个终止。

5月this可以帮助您

答案 1 :(得分:0)

这是我用来保持列顺序的方法。它有点乱,效率低但字符常数是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");
                    }
                }
            }