* 函数功能:读取txt文件到jTable2中
来源:互联网 发布:批处理编程 编辑:程序博客网 时间:2024/05/22 14:44
* 函数功能:读取txt文件到jTable2中
*
*/
public static void readCourseTxtFile(String filePath) {
try {
String encoding = "GBK";
File file = new File(filePath);
if (file.isFile() && file.exists()) {// 判断文件是否存在
InputStreamReader read = new InputStreamReader(
new FileInputStream(file), encoding);//考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
Vector data = new Vector();
int j = 0;
String[] temp;
while ((lineTxt = bufferedReader.readLine()) != null) {
temp = lineTxt.split(" ");
course[j] = new Course(temp[0], temp[1],
Double.parseDouble(temp[2]));
j += 1;
Vector row = new Vector();
for (int i = 0; i < temp.length; i++) {
row.add(temp[i]);
}
data.add(row);
}
for (int k = j; k < SIZE; k++) {
course[k] = null;
}
//String[] title=new String[]{"学号","姓名","性别","年龄","专业"};
Vector title = new Vector();
title.add("课程号");
title.add("课程名");
title.add("学分");
DefaultTableModel dtm = new DefaultTableModel(data, title);
jTable2.setModel(dtm);
read.close();
} else {
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
}
/**
* 修改课程表信息函数
* 创建时间:2014-02-20 13:17
* @param filePath
* @param Sno
*/
public static void UpdateCourseTxt(String filePath, Course st) {
String str = new String(); //原有txt内容
String s1 = new String();//内容更新
try {
File f = new File(filePath);
if (!f.exists()) {
System.out.print("文件不存在");
f.createNewFile();// 不存在则创建
}
BufferedReader input = new BufferedReader(new FileReader(f));
while ((str = input.readLine()) != null) {
String temp[] = str.split(" ");
if (temp[0].equals(st.GetCno())) {
str = st.GetCno() + " " + st.GetCname() + " "
+ st.GetCcredit();
}
s1 += str + "\n";
}
// System.out.println(s1);
input.close();
BufferedWriter output = new BufferedWriter(new FileWriter(f));
output.write(s1);
output.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//课程表添加按钮
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String Cno, Cname, Ccredit;
Cno = jTextField3.getText();
Cname = jTextField6.getText();
Ccredit = jTextField7.getText();
boolean b1 = hp.onlyCno(course, Cno);
boolean b2 = hp.onlyCname(course, Cname);
boolean b3 = hp.checkCcredit(Ccredit);
if (b1 && b2 && b3) {
String filePath = "D:\\数据结构课程设计\\Course.txt";
contentToTxt(filePath, Cno + " " + Cname + " " + Ccredit + "\n");
readCourseTxtFile(filePath);
jTextField3.setText("");
jTextField6.setText("");
jTextField7.setText("");
}
if (!b1) {
JOptionPane.showMessageDialog(this, "已经存在该课程号");
jTextField3.setText("");
}
if (!b2) {
JOptionPane.showMessageDialog(this, "已经存在该课程名");
jTextField6.setText("");
}
if (!b3) {
JOptionPane.showMessageDialog(this, "学分不符合规定,请重新输入");
jTextField7.setText("");
}
}
//课程表查找按钮
private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int k = 0;
String inputValue = JOptionPane.showInputDialog("请输入要查询课程的课程名");
if (inputValue != null) {
for (; course[k] != null; k++) {
if (course[k].GetCname()。equals(inputValue)) {
jTextField3.setText(course[k].GetCno());
jTextField6.setText(course[k].GetCname());
//注意,得把double值转化为String
jTextField7
.setText(Double.toString(course[k].GetCcredit()));
break;
}
}
if (course[k] == null) {
JOptionPane.showMessageDialog(this, "不存在该课程,请检查输入的课程名是否有误。");
}
}
}
//课程表删除按钮
private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
CourseTableSelected();
String filePath = "D:\\数据结构课程设计\\Course.txt";
String SCFilePath = "D:\\数据结构课程设计\\SC.txt";
//返回int值0&1,0表示确定,1表示否
int response = JOptionPane.showConfirmDialog(null, "是否真的删除?", "标题",
JOptionPane.YES_NO_OPTION);
if (response == 0) {
DeleteTxt(filePath, jTextField3.getText());
readCourseTxtFile(filePath);
DeleteSCTxtByNo(SCFilePath, jTextField3.getText(), 1);
readSCTxtFile(SCFilePath);
jTextField3.setText("");
jTextField6.setText("");
jTextField7.setText("");
jTextField3.setEditable(true);
jTextField6.setEditable(true);
JOptionPane.showMessageDialog(this, "删除成功!");
} else {
jTextField3.setText("");
jTextField6.setText("");
jTextField7.setText("");
jTextField3.setEditable(true);
}
}
//课程表修改按钮
private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (jButton6.getText()。equals("修改")) {
CourseTableSelected();
jButton6.setText("保存");
} else {
//返回int值0&1,0表示确定,1表示否
int response = JOptionPane.showConfirmDialog(null, "是否进行修改?", "标题",
JOptionPane.YES_NO_OPTION);
//如果确定的话,执行对新数据的保存
if (response == 0) {
String Cno, Cname, Ccredit;
Cno = jTextField3.getText();
Cname = jTextField6.getText();
Ccredit = jTextField7.getText();
boolean b = hp.checkCcredit(Ccredit);
if (b) {
Course cs = new Course(Cno, Cname,
Double.parseDouble(Ccredit));
String filePath = "D:\\数据结构课程设计\\Course.txt";
UpdateCourseTxt(filePath, cs);
readCourseTxtFile(filePath);
JOptionPane.showMessageDialog(this, "保存成功!");
}
if (!b) {
JOptionPane.showMessageDialog(this, "学分输入有误,请重新输入");
jTextField7.setText("");
}
}
jTextField3.setText("");
jTextField6.setText("");
jTextField7.setText("");
jTextField3.setEditable(true);
jTextField6.setEditable(true);
jButton6.setText("修改");
}
}
(六)、对成绩表的操作
/**
* 对成绩表的操作
* @param args
*/
static SC sc[] = new SC[SIZE];
//成绩表把表格的选中行填到文本框中
private void SCTableSelected() {
// TODO add your handling code here:
int rowIndex = jTable3.getSelectedRow();
jTextField8.setText(jTable3.getValueAt(rowIndex, 0)。toString());
jTextField9.setText(jTable3.getValueAt(rowIndex, 1)。toString());
jTextField10.setText(jTable3.getValueAt(rowIndex, 2)。toString());
jTextField8.setEditable(false);
jTextField9.setEditable(false);
}
/**
* 修改成绩表信息函数
* 创建时间:2014-02-22 21:33
* @param filePath
* @param st
*/
public static void UpdateSCTxt(String filePath, SC st) {
String str = new String(); //原有txt内容
String s1 = new String();//内容更新
try {
File f = new File(filePath);
if (!f.exists()) {
System.out.print("文件不存在");
f.createNewFile();// 不存在则创建
}
BufferedReader input = new BufferedReader(new FileReader(f));
while ((str = input.readLine()) != null) {
String temp[] = str.split(" ");
if (temp[0].equals(st.GetSno()) && temp[1].equals(st.GetCno())) {
str = st.GetSno() + " " + st.GetCno() + " " + st.GetGrade();
}
s1 += str + "\n";
}
input.close();
BufferedWriter output = new BufferedWriter(new FileWriter(f));
output.write(s1);
output.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* 函数功能:读取txt文件到jTable3中
* 创建时间:2014-02-22 12:13
*
*/
public static void readSCTxtFile(String filePath) {
try {
String encoding = "GBK";
File file = new File(filePath);
if (file.isFile() && file.exists()) {// 判断文件是否存在
InputStreamReader read = new InputStreamReader(
new FileInputStream(file), encoding);//考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
int k = 0;
String lineTxt = null;
Vector data = new Vector();
String[] temp;
while ((lineTxt = bufferedReader.readLine()) != null) {
temp = lineTxt.split(" ");
sc[k] = new SC(temp[0], temp[1],
Double.parseDouble(temp[2]));
k += 1;
Vector row = new Vector();
for (int i = 0; i < temp.length; i++) {
row.add(temp[i]);
}
data.add(row);
}
for(int m=k;k<size;k++)< p="">
{
sc[k]=null;
}
//String[] title=new String[]{"学号","姓名","性别","年龄","专业"};
Vector title = new Vector();
title.add("学号");
title.add("课程号");
title.add("成绩");
DefaultTableModel dtm = new DefaultTableModel(data, title);
jTable3.setModel(dtm);
read.close();
} else {
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
}
/**
* 成绩表的删除函数
* 创建时间:2014-02-22 21:55
* @param filePath
* @param num
*/
public static void DeleteSCTxt(String filePath, String Sno, String Cno) {
String str = new String(); //原有txt内容
String s1 = new String();//内容更新
try {
File f = new File(filePath);
if (!f.exists()) {
System.out.print("文件不存在");
f.createNewFile();// 不存在则创建
}
BufferedReader input = new BufferedReader(new FileReader(f));
while ((str = input.readLine()) != null) {
String temp[] = str.split(" ");
if (temp[0].equals(Sno)&& temp[1].equals(Cno)) {
} else {
s1 += str + "\n";
}
}
// System.out.println(s1);
input.close();
BufferedWriter output = new BufferedWriter(new FileWriter(f));
output.write(s1);
output.close();
} catch (Exception e) {
1
}
}
/**
* 根据学号或课程号完成成绩表的删除函数
* 创建时间:2014-02-23 22:21
* @param filePath
* @param Sno
*/
public static void DeleteSCTxtByNo(String filePath, String no, int k) {
String str = new String(); //原有txt内容
String s1 = new String();//内容更新
try {
File f = new File(filePath);
if (!f.exists()) {
System.out.print("文件不存在");
f.createNewFile();// 不存在则创建
}
BufferedReader input = new BufferedReader(new FileReader(f));
while ((str = input.readLine()) != null) {
String temp[] = str.split(" ");
if (!temp[k].equals(no)) {
s1 += str + "\n";
}
}
// System.out.println(s1);
input.close();
BufferedWriter output = new BufferedWriter(new FileWriter(f));
output.write(s1);
output.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//删除成绩按钮
//成绩表的删除按钮
private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
SCTableSelected();
String filePath = "D:\\数据结构课程设计\\SC.txt";
//返回int值0&1,0表示确定,1表示否
int response = JOptionPane.showConfirmDialog(null, "是否真的删除?", "标题",
JOptionPane.YES_NO_OPTION);
if (response == 0) {
DeleteSCTxt(filePath, jTextField8.getText(), jTextField9.getText());
readSCTxtFile(filePath);
jTextField8.setText("");
jTextField9.setText("");
jTextField10.setText("");
jTextField8.setEditable(true);
jTextField9.setEditable(true);
JOptionPane.showMessageDialog(this, "删除成功!");
} else {
jTextField8.setText("");
jTextField9.setText("");
jTextField10.setText("");
jTextField8.setEditable(true);
jTextField9.setEditable(true);
}
}
//成绩表的修改按钮
//修改成绩按钮
private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (jButton10.getText()。equals("修改")) {
SCTableSelected();
jButton10.setText("保存");
} else {
//返回int值0&1,0表示确定,1表示否
int response = JOptionPane.showConfirmDialog(null, "是否进行修改?", "标题",
JOptionPane.YES_NO_OPTION);
//如果确定的话,执行对新数据的保存
if (response == 0) {
String Sno, Cno, Grade;
Sno = jTextField8.getText();
Cno = jTextField9.getText();
Grade = jTextField10.getText();
boolean b = hp.checkGrade(Grade);
if (b) {
SC sc = new SC(Sno, Cno, Double.parseDouble(Grade));
String filePath = "D:\\数据结构课程设计\\SC.txt";
UpdateSCTxt(filePath, sc);
readSCTxtFile(filePath);
JOptionPane.showMessageDialog(this, "保存成功!");
} else {
JOptionPane.showMessageDialog(this, "成绩值输入有误(0~100)");
jTextField10.setText("");
return;
}
}
jTextField8.setText("");
jTextField9.setText("");
jTextField10.setText("");
jTextField8.setEditable(true);
jTextField9.setEditable(true);
jButton10.setText("修改");
}
}
//成绩表的添加按钮
//添加成绩的按钮
private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String Sno, Cno, Grade;
Sno = jTextField8.getText();
Cno = jTextField9.getText();
Grade = jTextField10.getText();
boolean b1 = hp.existSno(student, Sno);
boolean b2 = hp.existCno(course, Cno);
boolean b3 = hp.checkGrade(Grade);
boolean b4=hp.checkOnlyOneSC(sc, Sno, Cno);
if (b1&&b2&&b3&&b4) {
String filePath = "D:\\数据结构课程设计\\SC.txt";
contentToTxt(filePath, Sno + " " + Cno + " " + Grade + "\n");
readSCTxtFile(filePath);
jTextField8.setText("");
jTextField9.setText("");
jTextField10.setText("");
}
if (!b1) {
JOptionPane.showMessageDialog(this, "不存在该同学,请检查输入是否有误");
jTextField8.setText("");
}
if (!b2) {
JOptionPane.showMessageDialog(this, "不存在该课程,请检查输入是否有误");
jTextField9.setText("");
}
if (!b3) {
JOptionPane.showMessageDialog(this, "成绩值输入有误(0~100)");
jTextField10.setText("");
}
if(!b4){
JOptionPane.showMessageDialog(this, "该生的该课程已有成绩,不能再次添加");
jTextField8.setText("");
jTextField9.setText("");
}
}
/**
- * 函数功能:读取txt文件到jTable2中
- 读取Txt文件到数据库中
- 读取mat文件中特定数据保存到txt中
- java读取TXT文件保存到数据库中
- 从txt文件中读取数据到二维数组
- qt 读取txt文件内容并保存到数组中
- java读取TXT文件保存到数据库中
- 使用python读取.txt文件并保存到Excel中
- txt文件的读取以及按规律写入到另一个txt文件中
- Java中读取txt文件
- 读取一个文件夹下所有的txt文件,并保存到一个txt文本中
- MATLAB读取txt文件,并逐行处理字符串,写入到新的txt中
- 读取txt文件实现搜索替换功能
- 读取txt文件显示到treeview上
- c++读取txt文件到string
- java读取Excel数据,然后写入到txt文件,并批量保存到oracle数据库中
- 编写程序,完成文件复制功能,即将a.txt文件内容复制到b.txt文件中去
- 从数据库中读取表中数据然后写到txt文件中
- 数据结构课程设计
- 100个开源C/C++项目中的bugs
- 音频队列播放(二)
- 京东上市了,员工有多少股票? .
- JS统计还可以输入多少字数
- * 函数功能:读取txt文件到jTable2中
- 黑马程序员笔记——Java集合类二
- shell之重定向
- 匿名方法 与 Lambda 表达式
- private void jButton
- 微软为Windows全球发展计划招募APP开发者
- 【笔试】超大文件里的数据去重排序;将log中MAC地址提取出来;取出数据库表T里第20行到29行的数据;如何判断一个二叉树B是不是二叉树A的子树?
- linux 统计文件字段并排序
- spring security <http> 配置介绍