Java 将JTable内容保存到文本文件并读取
我的jTable有问题。我在网上读了很多东西,但我仍然不能解决我的问题。当我单击jButton1时,调用此过程(将jtable保存到文本文件) 我希望,当程序运行时,它从上面的路径加载这个文件,因此我想使用Java 将JTable内容保存到文本文件并读取,java,swing,file-io,jtable,Java,Swing,File Io,Jtable,我的jTable有问题。我在网上读了很多东西,但我仍然不能解决我的问题。当我单击jButton1时,调用此过程(将jtable保存到文本文件) 我希望,当程序运行时,它从上面的路径加载这个文件,因此我想使用BufferedReader方法。顺便说一下,我不知道怎么做。你有什么想法吗?:)在这里你可以看到我的代码,我只做了它的一部分,因为我不知道如何继续 public void LoadMyTable()throws Exception { BufferedReader br = new Bu
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();
}
你需要这样的东西
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')或紧接着换行符的回车符中的任意一个终止的
可以帮助您这是我用来保持列有序的方法。这有点混乱和低效,但字符常量是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");
}
}
}
BufferedWriter bfw=new BufferedWriter(new FileWriter(
"Data.txt),;
对于(int i=0;i20){//20(字符长)是我选择用来生成每个值的常量
name=name.substring(0,20);
}else if(name.length()==20){
}否则{
字符串空格=”;
int diff=20-name.length();
而(差异>0){
空格=空格+“”;
差异--;
}
name=name.concat(空格);
}
写下(姓名);
bfw.写入(“\t”);
}
for(int i=0;i20){
name=name.substring(0,20);
}else if(name.length()==20){
}否则{
字符串空格=”;
int diff=20-name.length();
而(差异>0){
空格=空格+“”;
差异--;
}
name=name.concat(空格);
}
写下(姓名);
bfw.写入(“\t”);
}
}
}
btw您应该将bufferedreader的创建放在try catch中block@nachokk是:)但在此之前,我需要知道如何在while循环中加载数据,“line”是我包含每行数据的行吗?:)@EmmaRossignoli yep文本中的行噢,非常感谢!:)我想问你最后一件事。我想用这个-->“addRow(newobject[]{textlines};”来把每一行放在我的表中。“我怎样才能用正确的方式来做呢?”@EmmaRossignoli我在答案中加了一个链接,可能会帮你再次感谢你,非常好的家伙:)
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
}
}
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");
}
}
}