txt数据如何导入到JTable中TXT有信息:学号,姓名,年龄,性别,系 5个信息,求完整的Java代码

 我来答
匿名用户
2013-07-20
展开全部
package Swing;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class Help {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

new MyFrame();
}

}

class MyFrame extends JFrame {

/**
*
*/
private static final long serialVersionUID = 1L;

public MyFrame(){
GetInfo getTask = new GetInfo();
Vector<Vector<String>> stuInfo = getTask.getStuInfo();
Vector<String> head = getTask.getHead();

DefaultTableModel tableModel = new DefaultTableModel(stuInfo,head);

JScrollPane jsp = new JScrollPane(new JTable(tableModel));

this.getContentPane().add(jsp);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(350, 450);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
}

class GetInfo {
private Vector<Vector<String>> stuInfo = null ;
private Vector<String> head = null ;

public GetInfo(){

FileInputStream fis = null;
try {
fis = new FileInputStream(new File("stuInfo.txt"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
InputStreamReader isr=new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);

String info = null ;

try {
String [] str = br.readLine().split(",") ;
head = new Vector<String>();
for (int i = 0; i < str.length; i++) {
head.add(str[i]);
}
info = br.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

stuInfo = new Vector<Vector<String>>();
while(info != null ){
String [] param = info.split(",");
Vector<String> temp = new Vector<String>();
for (int i = 0; i <param.length; i++) {
temp.add(param[i]);
}
stuInfo.add(temp);
try {
info = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
}
}

public Vector<Vector<String>> getStuInfo() {
return stuInfo;
}

public Vector<String> getHead() {
return head;
}
}

注意,记事本中的逗号必须是英文输入法下的逗号
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式