0

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

2 回答 2

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') 或紧跟换行符的回车符中的任何一个终止的。

对你有帮助

于 2013-06-18T22:00:43.903 回答
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");
                    }
                }
            }
于 2015-02-07T00:43:41.893 回答