标签:按钮 变量 second 缓存 font extend als 产生 存储
实现一个考试系统(单机版)
用到的技术
1.Swing实现窗口的绘制(View视图层)
2.文件+I/O
3.Java基本应用
集合ArrayList HashSet HashMap
字符串String StringBuilder
随机数Random
异常处理try catch
面向对象的核心
类(内部类 匿名内部类)
属性 方法 构造 块
特征修饰符 staic 权限修饰符 private protected public
继承 聚合 依赖 抽象类(模板) 接口 ActionListener Runnable
线程 反射 注解
4.设计思想
可读性(缩进 层级 命名 静态常量)
冗余度(重复 几乎都做了封装)
性能问题()
方法设计(参数 返回值)
类和类之间的关系
设计模式 单例(饿汉式,懒汉式 生命周期托管) 模板
MySpring 管理对象 单例 IOC控制反转
缓存问题
MVC分层架构
package dao; import java.util.*; import domain.Question; import util.*; //@SuppressWarnings("unchecked") public class QuestionDao{ private QuestionFileReader reader=MySpring.getBean("util.QuestionFileReader"); private ArrayList<Question> questionBank=new ArrayList<Question>(reader.getQuestionBox()); public ArrayList<Question> getPaper(int count){ HashSet<Question> qhs=new HashSet<Question>();//用来存储最终的试卷题目 //System.out.println(questionBank.size()); Random r=new Random();//随机数 while(qhs.size()!=count){ int index=r.nextInt(this.questionBank.size());//随机产生的一个题目索引位置 qhs.add(this.questionBank.get(index)); //System.out.println(qhs.size()); } return new ArrayList<Question>(qhs); } }
package dao; import java.util.*; import domain.User; import util.*; public class UserDao{ private UserFileReader ufr=MySpring.getBean("util.UserFileReader"); public User selectOne(String name){ return ufr.getUser(name); } } /* import domain.User; import dao.*; import util.*; public class UserService{ //private UserDao dao=new UserDao(); private UserDao dao=MySpring.getBean("dao.UserDao"); public String login(String account,String password){ User dbresult=dao.selectOne(account); if(dbresult!=null){ if(dbresult.getPassword().equals(password)){ return "登录成功"; } } return "登录失败"; } } public class UserFileReader{ private static HashMap<String,User> userBox=new HashMap<>(); public static User getUser(String account){ return userBox.get(account); } */
package domain; //存储文件中的题目,增强可读性 public class Question{ private String question;//题干 private String answer;//答案 private String pic;//存储图片路径 public Question(){ } public Question(String question,String answer){ this.question=question; this.answer=answer; } public Question(String question,String answer,String pic){ this.question=question; this.answer=answer; this.pic=pic; } public void setQuestion(String question){ this.question=question; } public String getQuestion(){ return this.question; } public void setAnswer(String answer){ this.answer=answer; } public String getAnswer(){ return this.answer; } public void setPic(String pic){ this.pic=pic; } public String getPic(){ return this.pic; } public boolean equals(Object obj){ if(this==obj){ return true; } if(obj instanceof Question){ Question anotherQuestion=(Question)obj; String thisTitle=this.question.substring(0,this.question.indexOf("<br>")); String anotherTitle=anotherQuestion.question.substring(0,anotherQuestion.question.indexOf("<br>")); if(thisTitle.equals(anotherTitle)){ return true; } } return false; } @Override public int hashCode(){ String thisTitle=this.question.substring(0,this.question.indexOf("<br>")); return thisTitle.hashCode(); } }
package domain; //实体对象 //存储文件中的一行记录 //文件名----类名 //文件名一行记录--类的对象 //文件一行中的值--对象的属性对应 public class User{ private String account; private String password; public User(String account,String password){ this.account=account; this.password=password; } public User(){ } public void setAccount(String account){ this.account=account; } public String getAccount(){ return this.account; } public void setPassword(String password){ this.password=password; } public String getPassword(){ return this.password; } }
package service; import util.MySpring; import domain.Question; import dao.QuestionDao; import java.util.*; public class QuestionService{ private QuestionDao dao=MySpring.getBean("dao.QuestionDao"); public ArrayList<Question> getPaper(int count){ return dao.getPaper(count); } }
package service; import domain.User; import dao.*; import util.*; public class UserService{ private UserDao dao=MySpring.getBean("dao.UserDao"); public String login(String account,String password){ User dbresult=dao.selectOne(account); if(dbresult!=null){ if(dbresult.getPassword().equals(password)){ return "登录成功"; } } return "登录失败"; } }
package test; import dao.*; import service.*; import test0608.LoginFrame; import java.util.*; import domain.*; import util.*; public class Test{ public static void main(String[] args){ new LoginFrame("登录窗口"); //UserService us=new UserService(); //System.out.println(us.login("zs","333")); //String question="1.一下那个是java的基本数据类型?\n A.String B.Integer C.boolean D.Math"; //System.out.println(question); /*//判断自己写的equals,hasCode方法; String a="以下那个是java的基本数据类型?<br> "; String b="以下那个是java的基本数据类型?<br> "; Question aa=new Question(a,"D"); Question ba=new Question(b,"b"); System.out.println(aa.equals(ba)); System.out.println(aa.hashCode()==ba.hashCode()); QuestionService service=MySpring.getBean("service.QuestionService"); ArrayList<Question> paper=service.getPaper(5); int i=1; for(Question q:paper){ String title=q.getQuestion().replace("<br>","\n "); System.out.println(i++ +". "+title); } */ } }
package test0608; import javax.swing.*; import java.awt.*; import util.*; import java.util.*; import service.*; import domain.*; import java.awt.Image; import java.awt.event.*; public class ExamFrame extends BaseFrame{ //获取QuestionService对象; private QuestionService service=MySpring.getBean("service.QuestionService"); private ArrayList<Question> paper=service.getPaper(5);//生成试卷; //创建一个用于存储学生选择的答案的容器 private String[] answers=new String[paper.size()]; //创建几个变量,分别控制右侧个数 private int nowCount=0;//记录当前题目序号; private int totalCount=paper.size();//题目总数: private int answerCount=0; private int unanswerCount=totalCount;//未答题数; public ExamFrame(){ this.init(); } public ExamFrame(String title){ super(title); this.init(); } //创建一个线程对象 控制时间变化 private TimeControlThread timeControlThread=new TimeControlThread(); //创建一个变量 用来记录整数(分钟为单位) private int times=61; //添加3个panel区域的分割 private JPanel mainPanel=new JPanel();//答题主页面 private JPanel rightPannel=new JPanel();//右 private JPanel bottomPannel=new JPanel(); //添加主要答题组件 private JTextArea testArea=new JTextArea();//考试文本域 private JScrollPane scrollPane=new JScrollPane(testArea);//滚动条 //添加右侧信息的组件 private JLabel pic=new JLabel();//展示图片 private JLabel nowNum=new JLabel("当前题号:");//展示当前题号; private JLabel total=new JLabel("题目总数:"); private JLabel answer=new JLabel("已答题数:"); private JLabel unanswer=new JLabel("未答题数:"); private JTextField nowNumField=new JTextField("0"); private JTextField totalField=new JTextField("0"); private JTextField answerField=new JTextField("0"); private JTextField unanswerField=new JTextField("0"); private JLabel time=new JLabel("剩余答题时间");//倒计时 private JLabel realTime=new JLabel("00:00:00");//真实时间 //添加底部组件 private JButton aButton=new JButton("A"); private JButton bButton=new JButton("B"); private JButton cButton=new JButton("C"); private JButton dButton=new JButton("D"); private JButton prevButton=new JButton("上一题"); private JButton nextButton=new JButton("下一题"); private JButton submitButton=new JButton("提交试卷"); protected void setFontAndSoOn(){ //设置panel布局管理-->自定义 mainPanel.setLayout(null); mainPanel.setBackground(Color.LIGHT_GRAY); rightPannel.setLayout(null); bottomPannel.setLayout(null); //手动设置每一个组件的位置 字体 背景 scrollPane.setBounds(16,10,650,450); testArea.setFont(new Font("黑体",Font.BOLD,34)); testArea.setEnabled(false);//文本域中的文字不让编辑 //设置右边区域的位置 rightPannel.setBounds(680,10,300,550); rightPannel.setBorder(BorderFactory.createLineBorder(Color.GRAY)); //设置底部区域的位置 bottomPannel.setBounds(16,470,650,90); bottomPannel.setBorder(BorderFactory.createLineBorder(Color.GRAY)); //设置右边区域中每一个组件位置 大小 颜色 pic.setBounds(10,10,280,230); pic.setBorder(BorderFactory.createLineBorder(Color.GRAY)); pic.setIcon(null);//展示图片信息 // nowNum.setBounds(40,270,100,30); nowNum.setFont(new Font("黑体",Font.PLAIN,20)); nowNumField.setBounds(150,270,100,30); nowNumField.setFont(new Font("黑体",Font.BOLD,20)); nowNumField.setBorder(BorderFactory.createLineBorder(Color.GRAY)); nowNumField.setEnabled(false); nowNumField.setHorizontalAlignment(JTextField.CENTER);//居中 total.setBounds(40,310,100,30); total.setFont(new Font("黑体",Font.PLAIN,20)); totalField.setBounds(150,310,100,30); totalField.setFont(new Font("黑体",Font.BOLD,20)); totalField.setBorder(BorderFactory.createLineBorder(Color.GRAY)); totalField.setEnabled(false); totalField.setHorizontalAlignment(JTextField.CENTER);//居中 answer.setBounds(40,350,100,30); answer.setFont(new Font("黑体",Font.PLAIN,20)); answerField.setBounds(150,350,100,30); answerField.setFont(new Font("黑体",Font.BOLD,20)); answerField.setBorder(BorderFactory.createLineBorder(Color.GRAY)); answerField.setEnabled(false); answerField.setHorizontalAlignment(JTextField.CENTER);//居中 unanswer.setBounds(40,390,100,30); unanswer.setFont(new Font("黑体",Font.PLAIN,20)); unanswerField.setBounds(150,390,100,30); unanswerField.setFont(new Font("黑体",Font.BOLD,20)); unanswerField.setBorder(BorderFactory.createLineBorder(Color.GRAY)); unanswerField.setEnabled(false); unanswerField.setHorizontalAlignment(JTextField.CENTER);//居中 unanswer.setBounds(40,390,100,30); unanswer.setFont(new Font("黑体",Font.PLAIN,20)); unanswerField.setBounds(150,390,100,30); unanswerField.setFont(new Font("黑体",Font.BOLD,20)); unanswerField.setBorder(BorderFactory.createLineBorder(Color.GRAY)); unanswerField.setEnabled(false); unanswerField.setHorizontalAlignment(JTextField.CENTER);//居中 time.setBounds(90,460,150,30); time.setFont(new Font("黑体",Font.PLAIN,20)); time.setForeground(Color.BLUE); realTime.setBounds(108,490,150,30); realTime.setFont(new Font("黑体",Font.BOLD,20)); realTime.setForeground(Color.BLUE); aButton.setBounds(40,10,120,30); aButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));//小手图标 bButton.setBounds(190,10,120,30); bButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));//小手图标 cButton.setBounds(340,10,120,30); cButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));//小手图标 dButton.setBounds(490,10,120,30); dButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));//小手图标 prevButton.setBounds(40,50,100,30); prevButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));//小手图标 nextButton.setBounds(510,50,100,30); nextButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));//小手图标 submitButton.setBounds(276,50,100,30); submitButton.setForeground(Color.RED); submitButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));//小手图标 //---------------------------------- //展示考试题目 this.showQuestionAndPic(); //重写设置右侧组件值; nowNumField.setText(nowCount+1+""); totalField.setText(totalCount+""); answerField.setText(answerCount+""); unanswerField.setText(unanswerCount+""); realTime.setText(times+""); } //属性添加到窗体 protected void addElement(){ rightPannel.add(pic); rightPannel.add(nowNum); rightPannel.add(nowNumField); rightPannel.add(total); rightPannel.add(totalField); rightPannel.add(answer); rightPannel.add(answerField); rightPannel.add(unanswer); rightPannel.add(unanswerField); rightPannel.add(time); rightPannel.add(realTime); bottomPannel.add(aButton); bottomPannel.add(bButton); bottomPannel.add(cButton); bottomPannel.add(dButton); bottomPannel.add(prevButton); bottomPannel.add(nextButton); bottomPannel.add(submitButton); mainPanel.add(scrollPane); mainPanel.add(rightPannel); mainPanel.add(bottomPannel); this.add(mainPanel); } //添加事件监听 protected void addListener(){ //创建一个监听器对象 用于提交按钮 submitButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ //弹出一个确认框 是0 否1 取消2 int value=JOptionPane.showConfirmDialog(ExamFrame.this,"是否确定提交试卷"); if(value==0){ //倒计时时间停止--线程处理 //timeControlThread.stop(); timeControlThread.stopTimeThread();//切换了线程的状态 //所有按钮失效; ExamFrame.this.setOptionButtonEnabled(false); prevButton.setEnabled(false); nextButton.setEnabled(false); //让按钮颜色回归本色 ExamFrame.this.clearOptionButtonColor(); //最终成绩的计算 展示在中间的文本域中 float score=ExamFrame.this.checkPaper(); testArea.setText("考试已经结束\n您最终的成绩为:"+score); } } }); //创建一个监听器对象 用于上一题按钮 prevButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ ExamFrame.this.clearOptionButtonColor(); //还原四个选项按钮的状态 变成可用 ExamFrame.this.setOptionButtonEnabled(true);//可用 //设置下一题按钮 变成可用 nextButton.setEnabled(true);//可用 //题号减1 nowCount--; //如果当前题号为0 已经到达第一题 让上一题按钮禁用 if(nowCount==0){ prevButton.setEnabled(false);//禁用 } //还原之前这道题选择的是哪一个选项 ExamFrame.this.restoreButton(); //显示题目 ExamFrame.this.showQuestionAndPic(); //修改右侧题号 nowNumField.setText(nowCount+1+""); answerField.setText(--answerCount+""); unanswerField.setText(++unanswerCount+""); } }); //监听器 用于下一题 nextButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ //先清除所有选项的颜色 ExamFrame.this.clearOptionButtonColor(); //当前题目需要增加一个 nowCount++; //当前题目是否到达最后 if(nowCount==totalCount){//到达最后一道题了 testArea.setText("全部题目已经回答完毕\n请点击下方红色提交按钮"); //让下一个题目按钮失效 nextButton.setEnabled(false);//不可用 //全部选项按钮失效 ExamFrame.this.setOptionButtonEnabled(false); }else{//后面还有题目 ExamFrame.this.showQuestionAndPic(); //修改右侧的当前题号 nowNumField.setText(nowCount+1+""); } answerField.setText(++answerCount+""); unanswerField.setText(--unanswerCount+""); } }); //用于四个选项按钮 ActionListener optionListener=new ActionListener(){ public void actionPerformed(ActionEvent e){ //清除之前所有选项按钮的颜色 ExamFrame.this.clearOptionButtonColor(); //让被点中的按钮颜色变化一下 JButton button=(JButton)e.getSource();//获取按钮 button.setBackground(Color.YELLOW); //选项存储在answers数组中 answers[nowCount]=button.getText(); } }; //将这个监听器对象绑定在四个选项按钮身上 aButton.addActionListener(optionListener); bButton.addActionListener(optionListener); cButton.addActionListener(optionListener); dButton.addActionListener(optionListener); } //设置窗体自身 protected void setFrameSelf(){ this.setBounds(260,130,1000,600); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setResizable(false); this.setVisible(true); timeControlThread.start();//启动计时线程 } /* public static void main(String[] args){ new ExamFrame("考试窗口"); } */ //---------------------------------------------------------- //设计一个内部类 处理时间倒计时问题 private class TimeControlThread extends Thread{ private boolean flag=true;//此时正常执行线程处理; public void stopTimeThread(){ this.flag=false; } public void run(){ //times进行一个转化 小时:分钟:秒 int hour=times/60; int minute=times%60; int second=0; while(flag){ //操作realTime处理时间的显示 StringBuilder timeString=new StringBuilder(); if(hour>=0 && hour<10){ timeString.append("0"); } timeString.append(hour); timeString.append(":"); if(minute>=0 && minute<10){ timeString.append("0"); } timeString.append(minute); timeString.append(":"); if(second>=0 && second<10){ timeString.append("0"); } timeString.append(second); //展示拼接以后的字符串 /* SwingUtilities.invokeLater(new Runnable(){ public void run(){ realTime.setText(timeString.toString()); } }); */ realTime.setText(timeString.toString()); try{ Thread.sleep(1000); }catch(Exception e){ e.printStackTrace(); } //改变 if(second>0){ second--; }else{//秒数已经为0 if(minute>0){//分钟>0 minute--; second=59; }else{//分钟已经为0 if(hour>0){//小时>0 hour--; minute=59; second=59; }else{//小时为0 System.out.println("时间截止"); realTime.setForeground(Color.RED); ExamFrame.this.setOptionButtonEnabled(false);//所有按钮失效; prevButton.setEnabled(false); nextButton.setEnabled(false); //弹出一个消息框,告诉考试结束,请提交 JOptionPane.showMessageDialog(ExamFrame.this,"考试结束,请提交试卷"); break; } } } } } } //设计一个方法 负责计算最终的成绩 private float checkPaper(){ float score=100; int size=paper.size(); for(int i=0;i<size;i++){//paper是一个ArrayList<Question> 题干 答案 图片路径 Question question=paper.get(i); String realAnswer=question.getAnswer(); if(!realAnswer.equals(answers[i])){ score-=(100/size); } } return score; } //还原上一题的选项 private void restoreButton(){ //获取当前题目的答案 String answer=answers[nowCount]; if(answer==null){ return; } switch(answer){ case "A": aButton.setBackground(Color.YELLOW); break; case "B": bButton.setBackground(Color.YELLOW); break; case "C": cButton.setBackground(Color.YELLOW); break; case "D": dButton.setBackground(Color.YELLOW); break; default: this.clearOptionButtonColor(); break; } } //让所有选项按钮失效 private void setOptionButtonEnabled(boolean key){ aButton.setEnabled(key); bButton.setEnabled(key); cButton.setEnabled(key); dButton.setEnabled(key); } //清除所有选项按钮颜色 private void clearOptionButtonColor(){ aButton.setBackground(null); bButton.setBackground(null); cButton.setBackground(null); dButton.setBackground(null); } //设计一个方法,处理图片展示 private ImageIcon drawImage(String path){ ImageIcon imageIcon=new ImageIcon(path); //设置imageIcon对象内的image属性 imageIcon.setImage(imageIcon.getImage().getScaledInstance(280,230,Image.SCALE_DEFAULT)); //返回imageIcon对象 return imageIcon; } //设计一个方法,用来展示一道题目 private void showQuestionAndPic(){ Question question=paper.get(nowCount); //获取当前question中的图片路径 String picpath=question.getPic();//图片路径 if(picpath!=null){//有图片 pic.setIcon(this.drawImage("img\\"+picpath)); }else{ pic.setIcon(null); } //处理一个题目中的标记<br>(表示换行) testArea.setText(nowCount+1+"."+question.getQuestion().replace("<br>","\n ")); } }
package test0608; import javax.swing.*; import java.awt.Font; import java.awt.Color; import java.awt.event.*; import util.BaseFrame; import service.UserService; import util.*; public class LoginFrame extends BaseFrame{ public LoginFrame(){ this.init(); } public LoginFrame(String title){ super(title); this.init(); } //面板 protected JPanel jpanel=new JPanel(); //创建title显示标题 protected JLabel bigTitle=new JLabel("在 线 考 试 系 统"); //账号 密码 protected JLabel accountLable=new JLabel("账户"); protected JLabel passwordLable=new JLabel("密码"); //文本框 protected JTextField accountField=new JTextField(); //密码框 protected JPasswordField passwordField=new JPasswordField(); //按钮 protected JButton login=new JButton("登录"); protected JButton exit=new JButton("退出"); protected void setFontAndSoOn(){ //设置组件的位置 布局管理 //边界式 BordLayout(JFrame) 流式FlowLayout(JPanel) //网格式GridLayout 自定义(null) //设置面板的布局管理为自定义方式 jpanel.setLayout(null); jpanel.setBackground(Color.WHITE); //设置标题组件的位置 bigTitle.setBounds(120,40,340,35); //设置字体大小 bigTitle.setFont(new Font("黑体",Font.BOLD,34)); //设置用户名位置,字体大小 accountLable.setBounds(94,120,90,30); accountLable.setFont(new Font("黑体",Font.BOLD,24)); //文本框 accountField.setBounds(204,120,260,30); accountField.setFont(new Font("黑体",Font.BOLD,24)); //密码 passwordLable.setBounds(94,180,90,30); passwordLable.setFont(new Font("黑体",Font.BOLD,24)); passwordField.setBounds(204,180,260,30); passwordField.setFont(new Font("黑体",Font.BOLD,24)); //登录 退出 login.setBounds(204,240,90,30); login.setFont(new Font("黑体",Font.BOLD,24)); exit.setBounds(330,240,90,30); exit.setFont(new Font("黑体",Font.BOLD,24)); } protected void addElement(){ jpanel.add(bigTitle); jpanel.add(accountLable); jpanel.add(accountField); jpanel.add(passwordLable); jpanel.add(passwordField); jpanel.add(login); jpanel.add(exit); this.add(jpanel); } protected void addListener(){ //绑定时间监听器 //ActionListener alistener=new Listener(); //login.addActionListener(alistener);//观察者模式 ActionListener alistener=new ActionListener(){ public void actionPerformed(ActionEvent e){ String account=accountField.getText(); //String password=passwordField.getText(); char[] value=passwordField.getPassword(); String pwd=new String(value); //UserService uservice=new UserService(); UserService uservice=MySpring.getBean("service.UserService"); String result=uservice.login(account,pwd); System.out.println(result); if(result.equals("登录成功")){ LoginFrame.this.setVisible(false);//将登录窗口隐藏 //System.out.println("成功啦"); new ExamFrame(account+"考试窗口"); }else{ JOptionPane.showMessageDialog(LoginFrame.this,result); //设置文本框和密码框为空 accountField.setText(""); passwordField.setText(""); //System.out.println("失败啦"); } } };//匿名内部类 login.addActionListener(alistener); } protected void setFrameSelf(){ //设置窗体起始位置 this.setBounds(400,200,680,320); //设置点击关闭退出程序 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设置窗体大小不可拖拽 this.setResizable(false); //设置窗体显示状态 this.setVisible(true); } }
package util; import javax.swing.*; public abstract class BaseFrame extends JFrame{ //模板模式 public BaseFrame(){ } public BaseFrame(String title){ super(title); } protected void init(){ this.setFontAndSoOn(); this.addElement(); this.addListener(); this.setFrameSelf(); } //字体 颜色。。 protected abstract void setFontAndSoOn(); //属性添加到窗体 protected abstract void addElement(); //添加事件监听 protected abstract void addListener(); //设置窗体自身 protected abstract void setFrameSelf(); }
package util; import java.util.*; @SuppressWarnings("unchecked") //目的是为了管理对象的产生 //对象的控制权交给当前类来负责 IOC控制反转 public class MySpring{ //属性 为了存储所有被管理的对象 private static HashMap<String,Object> beanBox=new HashMap<String,Object>(); //设计一个方法 获取任何一个类的对象 //返回值(泛型) 参数String类名 public static <T>T getBean(String className){//传递一个类全名 //通过类名字获取Class T obj=null; try{ obj=(T)beanBox.get(className); if(obj==null){ Class clazz=Class.forName(className); obj=(T)clazz.newInstance(); beanBox.put(className,obj); } }catch(Exception e){ e.printStackTrace(); } return obj; } }
package util; import java.util.*; import domain.Question; import java.io.*; public class QuestionFileReader{ private static HashSet<Question> questionBox=new HashSet<>(); static{ BufferedReader br=null; try{ File f=new File("dbfile\\Question.txt"); FileReader fr=new FileReader(f); br=new BufferedReader(fr); String readValue=br.readLine();//每一次读取一行 while(readValue!=null){ String[] readValues=readValue.split("#"); if(readValues.length==2){//无图片 questionBox.add(new Question(readValues[0],readValues[1])); }else if(readValues.length==3){//含图片 questionBox.add(new Question(readValues[0],readValues[1],readValues[2])); } readValue=br.readLine(); } }catch(Exception e){ e.printStackTrace(); }finally{ if(br!=null){ try{ br.close(); }catch(Exception e){ e.printStackTrace(); } } } } public HashSet<Question> getQuestionBox(){ return this.questionBox; } }
package util; import domain.User; import java.util.HashMap; import java.io.*; //目的是为了增加一个缓存机制 //程序启动的时候将User.txt文件中的所有信息 一次性读出来 //以后做查询直接读取缓存中的数据 提高读取的性能 public class UserFileReader{ private static HashMap<String,User> userBox=new HashMap<>(); public static User getUser(String account){ return userBox.get(account); } static{ BufferedReader reader=null; try{ reader=new BufferedReader(new FileReader("dbfile\\User.txt")); String message=reader.readLine();//每一次读取一行信息 while(message!=null){ String[] values=message.split("-");//一行记录的两个信息 userBox.put(values[0],new User(values[0],values[1])); message=reader.readLine(); } }catch(Exception e){ e.printStackTrace(); }finally{ if(reader!=null){ try{ reader.close(); }catch(Exception e){ e.printStackTrace(); } } } } }
标签:按钮 变量 second 缓存 font extend als 产生 存储
原文地址:https://www.cnblogs.com/canglongdao/p/13081572.html