JavaBean
package bean; public class Question { private String timu; //题干 private String leixing; //类型 private String axuanxiang; //A选项 private String bxuanxiang; //B选项 private String cxuanxiang; //C选项 private String dxuanxiang; //D选项 private String daan; //答案 private String jiexi; //解析 public String getTimu() { return timu; } public void setTimu(String timu) { this.timu = timu; } public String getLeixing() { return leixing; } public void setLeixing(String leixing) { this.leixing = leixing; } public String getAxuanxiang() { return axuanxiang; } public void setAxuanxiang(String axuanxiang) { this.axuanxiang = axuanxiang; } public String getBxuanxiang() { return bxuanxiang; } public void setBxuanxiang(String bxuanxiang) { this.bxuanxiang = bxuanxiang; } public String getCxuanxiang() { return cxuanxiang; } public void setCxuanxiang(String cxuanxiang) { this.cxuanxiang = cxuanxiang; } public String getDxuanxiang() { return dxuanxiang; } public void setDxuanxiang(String dxuanxiang) { this.dxuanxiang = dxuanxiang; } public String getDaan() { return daan; } public void setDaan(String daan) { this.daan = daan; } public String getJiexi() { return jiexi; } public void setJiexi(String jiexi) { this.jiexi = jiexi; } @Override public String toString() { return "Question [timu=" + timu + ", leixing=" + leixing + ", axuanxiang=" + axuanxiang + ", bxuanxiang=" + bxuanxiang + ", cxuanxiang=" + cxuanxiang + ", dxuanxiang=" + dxuanxiang + ", daan=" + daan + ", jiexi=" + jiexi + "]"; } public Question(String timu, String leixing, String axuanxiang, String bxuanxiang, String cxuanxiang, String dxuanxiang, String daan, String jiexi) { super(); this.timu = timu; this.leixing = leixing; this.axuanxiang = axuanxiang; this.bxuanxiang = bxuanxiang; this.cxuanxiang = cxuanxiang; this.dxuanxiang = dxuanxiang; this.daan = daan; this.jiexi = jiexi; } public Question() { super(); } }
读取txt封装对象的Java
1 package Action.txt; 2 3 import java.io.BufferedReader; 4 import java.io.BufferedWriter; 5 import java.io.FileInputStream; 6 import java.io.FileOutputStream; 7 import java.io.FileReader; 8 import java.io.InputStreamReader; 9 import java.io.OutputStreamWriter; 10 import java.io.Reader; 11 import java.util.ArrayList; 12 import java.util.List; 13 import java.util.regex.Pattern; 14 15 import org.apache.log4j.Logger; 16 import org.junit.Test; 17 18 import bean.Question; 19 20 /** 21 * 读取word上传上来的题目时候在最后多出一道空题,读取记事本编辑的上传上来的正常 22 * 23 * @author: qlq 24 * @date : 2017年7月25日上午9:04:12 25 */ 26 public class ReadTxtData { 27 private static Logger log = Logger.getLogger(ReadTxtData.class); 28 private static List<Question> list = new ArrayList<Question>(); 29 30 /** 31 * 传下来txt文件路径与名字,返回list<Question> 32 *@param fileName 33 *@return 34 *@throws Exception 35 */ 36 public static List<Question> readTxtData(String fileName) throws Exception { 37 String str; 38 FileInputStream fis; 39 // "E:\EclipseWorkspace\FreeMarker\test.txt" 40 fis = new FileInputStream(fileName); // 打开文件输入流 41 BufferedReader in = new BufferedReader(new InputStreamReader(fis, "utf-8")); 42 43 // 用于保存实际读取的字符数 44 45 StringBuffer sb = new StringBuffer(); 46 // 使用循环读取数据 47 String line = ""; 48 while ((line = in.readLine()) != null) { 49 if (!line.equals("")) { 50 sb.append(line); 51 } 52 } 53 54 String all = sb.toString().trim(); 55 /* System.out.println(all); 56 System.out.println("--------------------------------------");*/ 57 // System.out.println(all.trim()); 58 // System.out.println("--------------------------------------"); 59 60 List<Question> list = new ArrayList<>(); 61 62 // 分割题,以[题干]分割,注意第一个前面还有一道空的 63 String ti[] = all.split("\[题干\]"); 64 // System.out.println(ti.length); 65 // substring(start,end) 简单理解为从int开始取end-start个 66 for (int i=1;i<ti.length;i++) { 67 Question question = new Question(); 68 // 提取题干 69 String tigan = ti[i].substring(0, ti[i].indexOf("[类型]")); 70 question.setTimu(tigan); 71 // System.out.println(tigan); 72 // 提取类型 73 int leixing_start = ti[i].indexOf("[类型]")+4; 74 int leixing_end = leixing_start+2; 75 String leixing = ti[i].substring(leixing_start,leixing_end); 76 question.setLeixing(leixing); 77 // System.out.println(leixing); 78 // 提取ABCD 79 if(leixing.equals("判断")){ //如果是判断题 80 // 提取答案,从[答案]后开始,取一个 81 int daan_start = ti[i].indexOf("[答案]")+4; 82 String daan = ti[i].substring(daan_start, daan_start+1); 83 question.setDaan(daan); 84 // System.out.println(daan); 85 // 提取解析 86 int jiexi_start = ti[i].indexOf("[解析]")+4; 87 String jiexi = ti[i].substring(jiexi_start); 88 question.setJiexi(jiexi); 89 // System.out.println(jiexi); 90 }else{ 91 // 提取ABCD选项: 92 int a_start = ti[i].indexOf("[A选项]")+5; 93 int a_end = ti[i].indexOf("[B选项]"); 94 String axuanxiang=ti[i].substring(a_start, a_end); 95 question.setAxuanxiang(axuanxiang); 96 // System.out.println(axuanxiang); 97 98 int b_start = ti[i].indexOf("[B选项]")+5; 99 int b_end = ti[i].indexOf("[C选项]"); 100 String bxuanxiang=ti[i].substring(b_start, b_end); 101 question.setBxuanxiang(bxuanxiang); 102 // System.out.println(bxuanxiang); 103 104 int c_start = ti[i].indexOf("[C选项]")+5; 105 int c_end = ti[i].indexOf("[D选项]"); 106 String cxuanxiang=ti[i].substring(c_start, c_end); 107 question.setCxuanxiang(cxuanxiang); 108 // System.out.println(cxuanxiang); 109 110 int d_start = ti[i].indexOf("[D选项]")+5; 111 int d_end = ti[i].indexOf("[答案]"); 112 String dxuanxiang=ti[i].substring(d_start, d_end); 113 question.setDxuanxiang(dxuanxiang); 114 // System.out.println(dxuanxiang); 115 116 // 提取答案 117 int daan_start = ti[i].indexOf("[答案]")+4; 118 int daan_end = ti[i].indexOf("[解析]"); 119 String daan = ti[i].substring(daan_start, daan_end); 120 question.setDaan(daan); 121 // System.out.println(daan); 122 // 提取解析 123 int jiexi_start = ti[i].indexOf("[解析]")+4; 124 String jiexi = ti[i].substring(jiexi_start); 125 question.setJiexi(jiexi); 126 // System.out.println(jiexi); 127 } 128 list.add(question); 129 } 130 return list; 131 132 } 133 134 @Test 135 public void test1() throws Exception{ 136 List<Question> list1 = ReadTxtData.readTxtData("C:/Users/liqiang/Desktop/1.txt"); 137 System.out.println(list1); 138 } 139 140 141 }
结果:
[Question [timu=你喜欢什么, leixing=单选, axuanxiang=方便吗, bxuanxiang=垃圾, cxuanxiang=牛肉, dxuanxiang=大饼, daan=A, jiexi=因为我喜欢大饼], Question [timu=你喜欢什么, leixing=多选, axuanxiang=篮球, bxuanxiang=足球, cxuanxiang=羽毛球, dxuanxiang=美女, daan=ABCD, jiexi=我都喜欢], Question [timu=你是人, leixing=判断, axuanxiang=null, bxuanxiang=null, cxuanxiang=null, dxuanxiang=null, daan=对, jiexi=因为我是人], Question [timu=你喜欢什么, leixing=单选, axuanxiang=方便吗, bxuanxiang=垃圾, cxuanxiang=牛肉, dxuanxiang=大饼, daan=A, jiexi=因为我喜欢大饼], Question [timu=你喜欢什么, leixing=多选, axuanxiang=篮球, bxuanxiang=足球, cxuanxiang=羽毛球, dxuanxiang=美女, daan=ABCD, jiexi=我都喜欢], Question [timu=你是人, leixing=判断, axuanxiang=null, bxuanxiang=null, cxuanxiang=null, dxuanxiang=null, daan=对, jiexi=因为我是人], Question [timu=你喜欢什么, leixing=单选, axuanxiang=方便吗, bxuanxiang=垃圾, cxuanxiang=牛肉, dxuanxiang=大饼, daan=A, jiexi=因为我喜欢大饼], Question [timu=你喜欢什么, leixing=多选, axuanxiang=篮球, bxuanxiang=足球, cxuanxiang=羽毛球, dxuanxiang=美女, daan=ABCD, jiexi=我都喜欢], Question [timu=你是人, leixing=判断, axuanxiang=null, bxuanxiang=null, cxuanxiang=null, dxuanxiang=null, daan=对, jiexi=因为我是人], Question [timu=你喜欢什么, leixing=单选, axuanxiang=方便吗, bxuanxiang=垃圾, cxuanxiang=牛肉, dxuanxiang=大饼, daan=A, jiexi=因为我喜欢大饼], Question [timu=你喜欢什么, leixing=多选, axuanxiang=篮球, bxuanxiang=足球, cxuanxiang=羽毛球, dxuanxiang=美女, daan=ABCD, jiexi=我都喜欢], Question [timu=你是人, leixing=判断, axuanxiang=null, bxuanxiang=null, cxuanxiang=null, dxuanxiang=null, daan=对, jiexi=因为我是人]]
1.txt
[题干]你喜欢什么
[类型]单选
[A选项]方便吗
[B选项]垃圾
[C选项]牛肉
[D选项]大饼
[答案]A
[解析]因为我喜欢大饼
[题干]你喜欢什么
[类型]多选
[A选项]篮球
[B选项]足球
[C选项]羽毛球
[D选项]美女
[答案]ABCD
[解析]我都喜欢
[题干]你是人
[类型]判断
[答案]对
[解析]因为我是人
[题干]你喜欢什么
[类型]单选
[A选项]方便吗
[B选项]垃圾
[C选项]牛肉
[D选项]大饼
[答案]A
[解析]因为我喜欢大饼
[题干]你喜欢什么
[类型]多选
[A选项]篮球
[B选项]足球
[C选项]羽毛球
[D选项]美女
[答案]ABCD
[解析]我都喜欢
[题干]你是人
[类型]判断
[答案]对
[解析]因为我是人
[题干]你喜欢什么
[类型]单选
[A选项]方便吗
[B选项]垃圾
[C选项]牛肉
[D选项]大饼
[答案]A
[解析]因为我喜欢大饼
[题干]你喜欢什么
[类型]多选
[A选项]篮球
[B选项]足球
[C选项]羽毛球
[D选项]美女
[答案]ABCD
[解析]我都喜欢
[题干]你是人
[类型]判断
[答案]对
[解析]因为我是人
[题干]你喜欢什么
[类型]单选
[A选项]方便吗
[B选项]垃圾
[C选项]牛肉
[D选项]大饼
[答案]A
[解析]因为我喜欢大饼
[题干]你喜欢什么
[类型]多选
[A选项]篮球
[B选项]足球
[C选项]羽毛球
[D选项]美女
[答案]ABCD
[解析]我都喜欢
[题干]你是人
[类型]判断
[答案]对
[解析]因为我是人