第十六天:用properties获取本地的txt信息,并按要求打印出来

来源:互联网 发布:袁世凯如果不称帝 知乎 编辑:程序博客网 时间:2024/05/22 02:14

控制台输出的形式如下:


1,本地文件

message.properties内容为

#key=value

questionFile=question.txt

userFile=user.txt

ruleFile=rule.txt

user.txt的内容为:

1000:张三:1234:18710577965:西安雁塔区3号
1001:李四:1234:18710577954:西安雁塔区5号
1002:李五:1234:18710577954:西安雁塔区5号

rule.txt的内容为:

1、考试期间不许查看手机,作弊
2、考试不允许私带书本进入考场
3、。。。。。。。。。。。。。

question.txt的内容为:

@answer=2/3,score=5,level=
指出下面语句没有编译错误的是:
long n = 999999999999;int n = 999999999999L;
long n = 999999999999L; 
double n = 999999999999;
@answer=2,score=5,level=4
下列关于数组的声明错误的是:
int[] arry = new int[100];
int[3] arry = {1,2,3};
int[] arry = new int[]{1,2,3};
int[][] arry = new int[3][];
@answer=0/1/2,score=5,level=3
语句System.out.println(1+2+"java"+3+4)输出的结果是:
3java34
12java34
3java7
12java7
@answer=1,score=5,level=6
Java语言中int类型标示整数的最大范围是:
-2147483647~ 2147483647 
<T>-2147483648 ~2147483647 
-32767~32767
-32768~32767
@answer=1,score=5,level=7
Java语言中字符串“学Java”所占的内存空间是:
6个字节
7个字节
10个字节
11个字节


user类:

package exercise01;public class User {private int id;private String name;private String pwd;private String phone;private String address;/** * @param id * @param name * @param pwd * @param phone * @param address */public User(int id, String name, String pwd, String phone, String address) {super();this.id = id;this.name = name;this.pwd = pwd;this.phone = phone;this.address = address;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getPwd() {return pwd;}public void setPwd(String pwd) {this.pwd = pwd;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPhone() {return phone;}public void setPhone(String phone) {this.phone = phone;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}@Overridepublic int hashCode() {final int prime = 31;int result = 1;result = prime * result + ((address == null) ? 0 : address.hashCode());result = prime * result + id;result = prime * result + ((name == null) ? 0 : name.hashCode());result = prime * result + ((phone == null) ? 0 : phone.hashCode());result = prime * result + ((pwd == null) ? 0 : pwd.hashCode());return result;}@Overridepublic boolean equals(Object obj) {if (this == obj)return true;if (obj == null)return false;if (getClass() != obj.getClass())return false;User other = (User) obj;if (address == null) {if (other.address != null)return false;} else if (!address.equals(other.address))return false;if (id != other.id)return false;if (name == null) {if (other.name != null)return false;} else if (!name.equals(other.name))return false;if (phone == null) {if (other.phone != null)return false;} else if (!phone.equals(other.phone))return false;if (pwd == null) {if (other.pwd != null)return false;} else if (!pwd.equals(other.pwd))return false;return true;}@Overridepublic String toString() {return "User [id=" + id + ", pwd=" + pwd + ", name=" + name+ ", phone=" + phone + ", address=" + address + "]";}}
Question类

package exercise01;import java.util.Arrays;public class Question {public static final int LEVEL1=1;public static final int LEVEL2=2;public static final int LEVEL3=3;public static final int LEVEL4=4;public static final int LEVEL5=5;public static final int SINGLE_TYPE=0;public static final int MULTIE_TYPE=1;private String title;private String[] options;private String[] rightAnswer;private int level; private String score;private int type;public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String[] getOptions() {return options;}public void setOptions(String[] options) {this.options = options;}public String[] getRightAnswer() {return rightAnswer;}public void setRightAnswer(String[] rightAnswer) {this.rightAnswer = rightAnswer;}public String getScore() {return score;}public void setScore(String score) {this.score = score;}public int getType() {return type;}public void setType(int type) {this.type = type;}public static int getLevel1() {return LEVEL1;}public static int getLevel2() {return LEVEL2;}public static int getLevel3() {return LEVEL3;}public static int getLevel4() {return LEVEL4;}public static int getLevel5() {return LEVEL5;}public static int getSingleType() {return SINGLE_TYPE;}public static int getMultieType() {return MULTIE_TYPE;}public int getLevel() {return level;}public void setLevel(int level) {this.level = level;}@Overridepublic int hashCode() {final int prime = 31;int result = 1;result = prime * result + level;result = prime * result + Arrays.hashCode(options);result = prime * result + Arrays.hashCode(rightAnswer);result = prime * result + ((score == null) ? 0 : score.hashCode());result = prime * result + ((title == null) ? 0 : title.hashCode());result = prime * result + type;return result;}@Overridepublic boolean equals(Object obj) {if (this == obj)return true;if (obj == null)return false;if (getClass() != obj.getClass())return false;Question other = (Question) obj;if (level != other.level)return false;if (!Arrays.equals(options, other.options))return false;if (!Arrays.equals(rightAnswer, other.rightAnswer))return false;if (score == null) {if (other.score != null)return false;} else if (!score.equals(other.score))return false;if (title == null) {if (other.title != null)return false;} else if (!title.equals(other.title))return false;if (type != other.type)return false;return true;}@Overridepublic String toString() {return "\n"+"(" + score+"分)" +"题目:" + title +" \n" +"选项:"+ Arrays.toString(options);}}
逻辑类:

package exercise01;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.io.UnsupportedEncodingException;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Properties;public class EntityContext {private List<User> listUsers=new ArrayList<User>();private Map<Integer,List<Question>> mapQuestion=new HashMap<Integer,List<Question>>();private StringBuilder ruleStr=new StringBuilder();static Properties pros=new Properties();static{try {pros.load(new FileInputStream(new File("message.properties")));} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public EntityContext() throws IOException{String userTxt=pros.getProperty("userFile");loadUser(userTxt);String questionTxt=pros.getProperty("questionFile");loadQuestions(questionTxt);String ruleTxt=pros.getProperty("ruleFile");loadRule(ruleTxt);}//解析考场纪律文件@SuppressWarnings("resource")private void loadRule(String ruleTxt) throws IOException{BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(new File(ruleTxt)),"GBK"));String str;while((str=br.readLine())!=null){if(str.equals(" ")||str.equals("")){continue;}ruleStr.append(str).append("\n");}}public String getRule(){return ruleStr.toString();}//解析考试文件private void loadQuestions(String questionTxt){BufferedReader br = null;try {br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(questionTxt)),"GBK"));} catch (UnsupportedEncodingException e1) {// TODO Auto-generated catch blocke1.printStackTrace();} catch (FileNotFoundException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}String str;try {while((str=br.readLine())!=null){if(str.equals("")||str.equals("  ")){continue;}Question q=praseQuestion(str,br);splitQuestion(q);}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public void getQuestion(int level){List<Question> list=mapQuestion.get(level);Iterator<Question> it=list.iterator();while(it.hasNext()){System.out.println(it.next());}}public Question praseQuestion(String str, BufferedReader br) throws IOException {// TODO Auto-generated method stubQuestion q=new Question();String title=br.readLine();String[] options = new String[4];options[0]=br.readLine();options[1]=br.readLine();options[2]=br.readLine();options[3]=br.readLine();//@answer=2/3,score=5,level=5String[] ary  =str.split("[@,][a-z]+=");String[] rightAnswer =ary[1].split("/");q.setTitle(title);q.setRightAnswer(rightAnswer);q.setScore(ary[2]);q.setLevel(Integer.parseInt(ary[3]));q.setOptions(options);q.setType(rightAnswer.length==1?Question.SINGLE_TYPE:Question.MULTIE_TYPE);return q;}public void splitQuestion(Question q) {// TODO Auto-generated method stubList<Question>  list=mapQuestion.get(q.getLevel());if(list==null){list=new ArrayList<Question>();}list.add(q);mapQuestion.put(q.getLevel(), list);}//解析用户信息@SuppressWarnings("resource")public void loadUser(String userTxt) throws IOException{BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(new File(userTxt)),"GBK"));String str;String[]  ary;while((str=br.readLine())!=null){if(str.equals("")||str.equals("  ")){continue;}ary=str.split(":");listUsers.add(new User(Integer.parseInt(ary[0]),ary[1],ary[2],ary[3],ary[4]));}}public String getUser(int id){return listUsers.get(id).toString();}}
Demo类:

package exercise01;import java.io.IOException;public class Main {public static void main(String[] args) throws IOException {EntityContext ec=new EntityContext();System.out.println(ec.getRule());ec.getQuestion(1);System.out.println("\n"+ec.getUser(1).toString());}}










阅读全文
0 0
原创粉丝点击