- 第一段代码是我从一个文件读出数据,并且将数据用#分割开来
public static String read(String filePath){
File file=new File(filePath);
FileReader fr=null;
BufferedReader br=null;
String content="";
String line=null;
try {
fr=new FileReader(file);
br=new BufferedReader(fr);
while((line=br.readLine())!=null){
content=content+"#"+line;
}
} catch (FileNotFoundException e) {
System.out.println("指定的文件不存在:"+file.getName());
e.printStackTrace();
return null;
} catch (IOException e) {
System.out.println("读取文件出错");
e.printStackTrace();
return null;
}finally{
if(br!=null){try {br.close();} catch (IOException e) {e.printStackTrace();}}
if(fr!=null){try{fr.close();}catch(IOException e){e.printStackTrace();}}
}
return content;
2.这段代码是我将txt装换为map,应为我读出来的数据是一道一道的题目,格式是 题目+4个选项+答案
public static List<Map<String, String>> txt2Map(String fileFrom) {
FileReadUtil fileReadUtil=new FileReadUtil();
String question[]=fileReadUtil.read(fileFrom).trim().split("#");
List<Map<String, String>> questions=new ArrayList<Map<String,String>>();
for(int i=1;i<question.length;i=i+6){
Map<String, String> map=new HashMap<String, String>();
map.put("title", question[i]);
map.put("A", question[i+1]);
map.put("B", question[i+2]);
map.put("C", question[i+3]);
map.put("D", question[i+4]);
map.put("anwser", question[i+5]);
questions.a