本次实验的测试数据(采用UTF-8模式,windows默认为GBK编码)
准考证号 姓名 性别 数学 语文 英语
LHGZ31025 王申银 男 99.0 95.0 93.0
LHGZ31005 王自立 男 95.0 88.0 97.0
LHGZ31012 周春国 男 67.0 95.0 88.0
LHGZ31017 李玲 女 85.5 95.0 97.5
LHGZ31020 郭卫华 男 69.0 76.0 66.0
LHGZ31007 郭建平 男 67.0 73.0 69.0
LHGZ31009 韩俊平 女 66.0 89.0 79.0
LHGZ31016 张磊 男 67.0 73.0 69.0
LHGZ31001 尚秋华 女 85.0 73.0 66.5
LHGZ31023 薛庆庆 男 66.0 90.0 55.0
LHGZ31021 张国方 男 67.0 73.0 69.0
LHGZ31003 张岩 男 66.5 78.0 89.0
LHGZ31014 许海峰 男 66.0 67.0 73.0
LHGZ31019 尹小聚 男 78.0 89.0 79.0
运行结果
将上述文件封装到Student类中
class Student{
String id;
String name;
String sex;
double math;
double chinese;
double english;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public double getMath() {
return math;
}
public void setMath(double math) {
this.math = math;
}
public double getChinese() {
return chinese;
}
public void setChinese(double chinese) {
this.chinese = chinese;
}
public double getEnglish() {
return english;
}
public void setEnglish(double english) {
this.english = english;
}
@Override
public String toString() {
return id+"\t"+name+"\t"+sex+"\t"+math+"\t"+chinese+"\t"+english+"\n";
}
}
将文本中的数据读取到接口中的方法
/**
/* 将文件中的内容读到集合中
/* @param c 接口
/* @param filePathAndName 文件路径及名称
/*/
public void readFile(Collection c,String filePathAndName){
Scanner sc=null;
String str=”“;//list中的内容
String line=”“;//每一行的内容
File f=new File(filePathAndName);
InputStreamReader read;
try {
read = new InputStreamReader(new FileInputStream(f),”UTF-8”);
BufferedReader reader=new BufferedReader(read);
reader.readLine();//跳过第一行
while((line=reader.readLine())!=null){
Scanner sc1=new Scanner(line);
while(sc1.hasNext()){
Student s=new Student();
s.setId(sc1.next());
s.setName(sc1.next());
s.setSex(sc1.next());
s.setMath(sc1.nextDouble());
s.setChinese(sc1.nextDouble());
s.setEnglish(sc1.nextDouble());
c.add(s);
}
}
}catch (Exception e1) {
e1.printStackTrace();
}finally{
if(sc!=null){
sc.close();
}
}
}
利用GUI制作的界面
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
import sy13.Student;//导入Student类
public class TT extends JFrame {
public JPanel p;
public JLabel lbl;
public JTextField input;
public JButton selBtn;
public JButton cloBtn;
public JScrollPane jsp;
public JTextArea output;
public ArrayList list;
public TT(){
p=new JPanel();
lbl=new JLabel();
lbl.setText(“请输入N:”);
input=new JTextField();
input.setColumns(10);
selBtn=new JButton(“查询”);
list=new ArrayList();
//查询功能的实现
selBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int num=Integer.parseInt(input.getText());
//将文件中的内容读取到集合中去
readFile(list,”test.txt”);
//查找的具体实现
int datanum=list.size();
Iterator iter=list.iterator();
if(num<=datanum){
//输入的记录数小于数据实际的记录数时
output.setText("");
output.setText("准考证号\t姓名\t性别\t数学\t语文\t英语\n");
for(int i=0;i<num;i++){
output.append(iter.next().toString());
}
}else{
output.setText("");
JOptionPane.showMessageDialog(null,"请输入的数字太大了,没有这么多条记录","消息",JOptionPane.INFORMATION_MESSAGE);
}
//清空list中的内容
list.clear();
}
});
cloBtn=new JButton("关闭");
//关闭按钮的实现
cloBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
dispose();
System.exit(0);
}
});
output=new JTextArea();
output.setColumns(45);
output.setRows(20);
output.setFont(new Font("STSong", Font.PLAIN, 12));
jsp=new JScrollPane();
jsp.setViewportView(output);
p.add(lbl);
p.add(input);
p.add(selBtn);
p.add(cloBtn);
p.add(jsp);
this.setFont(new Font("STSong", Font.PLAIN, 16));
getContentPane().add(p);
setBounds(300,200,450,450);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
}
补充:
一定要值得注意的是InputStreamReader(new FileInputStream(f),”UTF-8”);中的格式,在window平台下用GBK,否则会出现错误