读取txt文件转换成map,以及将map…

来源:互联网 发布:iphone通讯录恢复软件 编辑:程序博客网 时间:2024/05/02 00:19
  1. 第一段代码是我从一个文件读出数据,并且将数据用#分割开来
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(IOExceptione){e.printStackTrace();}}
}
return content;
2.这段代码是我将txt装换为map,应为我读出来的数据是一道一道的题目,格式是题目+4个选项+答案
 大家可以酌情考虑自己的情况,也可以读一行加一行,但是由于我是作业要求就不解释了。
public static List<Map<String,String>> txt2Map(String fileFrom){
FileReadUtil fileReadUtil=new FileReadUtil();
Stringquestion[]=fileReadUtil.read(fileFrom).trim().split("#");
List<Map<String,String>> questions=newArrayList<Map<String,String>>();
for(int i=1;i<question.length;i=i+6){
Map<String, String> map=newHashMap<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.add(map);
}
//System.out.println(questions);
return questions;

3.现在我们要把map写到xml中,让每一道题成为一个xml的记录

可以看到代码,(当然那些个syso可以无视)我是先从txt读成map然后再转成xml,实际上也就实现了txt到xml的转换。

package com.xml.paper;

import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

public class Map2XML {

public static void main(String[] args) {
map2Xml(TXT2Map.txt2Map("question.txt"),"question.xml");
}

public static voidmap2Xml(List<Map<String,String>> maps ,String fileTo){
//1.构造空的文档Document
Document doc=DocumentHelper.createDocument();
//2.构造根元素
Element rootElmt=doc.addElement("questions");
//3.递归构造子元素
for(Map<String, String>question:maps){
Element questionElmt
=rootElmt.addElement("question");
//为book元素增加属性
//bookElmt.addAttribute("isbn", book[0]);
//bookElmt.addAttribute("catalog", book[1]);
//book元素增加四个四元素
Element titleElement
=questionElmt.addElement("title");
//title元素设置数据
System.out.println(question.get("title"));
titleElement.setText(question.get("title").substring(2));
//title加属性
titleElement.addAttribute("id",question.get("title").substring(0,1));
Element AElement
=questionElmt.addElement("A");
System.out.println(question.get("A"));
AElement.setText(question.get("A"));
Element BElement
=questionElmt.addElement("B");
System.out.println(question.get("B"));
BElement.setText(question.get("B"));
Element CElement
=questionElmt.addElement("C");
System.out.println(question.get("C"));
CElement.setText(question.get("C"));
Element DElement
=questionElmt.addElement("D");
System.out.println(question.get("D"));
DElement.setText(question.get("D"));
Element anwserElement
=questionElmt.addElement("anwser");
System.out.println(question.get("anwser"));
anwserElement.setText(question.get("anwser"));
}
//4.输出成为一个文件
outPutXml(doc,fileTo);
}
private static void outPutXml(Document doc, String filename){
// TODO Auto-generated method stub
try {
//定义输出流的目的地
FileWriter fw=new FileWriter(filename);
//定义输出格式:创建一个合适的输出格式
   OutputFormatformat=OutputFormat.createPrettyPrint();
  format.setEncoding("GBK");
   
  //定义用于输出的xml文件的xmlwriter对象
   XMLWriter xmlWriter=newXMLWriter(fw,format);
  xmlWriter.write(doc);//**写出
  xmlWriter.close();//关闭
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

由于代码有注释我就不多解释了,
我们想要的xml的结构是这样的:
<?xml version="1.0"encoding="UTF-8"?>
<questions>
<question id="1">
<title>
</title>
<options>
<option></option>
<option></option>
<option></option>
<option></option>
</options>
<answer></answer>
<level></level>
<score></score>
</question>
</questions>

这是生成后的xml,这样我们就把txt的题目存成了xml,当然这是只针对这一种题目格式,而且如果题目中多一行少一行还是会影响使用的,所以要改进。
<?xml version="1.0"encoding="GBK"?>

<questions>
 <question>
    <titleid="1">下列关于String说法错误的是();</title>
   <A>A.字符串的长度用length();</A>
   <B>B.字符串可以用charAt()方法得到单个字符</B>
   <C>C.String字符串的大小是可以用Size()得到。</C>
   <D>D.所有的其他类型的类都可以覆盖toString()方法实现输出;</D>
   <anwser>C</anwser>
 </question>
 <question>
    <titleid="2">下列关于String,StringBuffer的判断错误的是();</title>
   <A>A.String是不可变字符串,StringBuffer是可变长度字符串。</A>
   <B>B.可以用equals方法检测两个字符串相等。</B>
   <C>C.String是包装类,不可以被继承。</C>
   <D>D.String类型允许用“+“拼接字符串。</D>
   <anwser>C</anwser>
 </question>
 <question>
    <titleid="3">下列选项里,哪些返回true?</title>
   <A>A.s.equals(t);</A>
   <B>B.t.equals(c);</B>
   <C>C.s==t;</C>
   <D>D. t.equals(newString("hello"));</D>
   <anwser>D</anwser>
 </question>
 <question>
    <titleid="4">下面String数组定义正确的是()。</title>
   <A>A.String strs[] = { 'a' 'b''c'};</A>
   <B>B.String[] strs = {"a" "b""c"};</B>
   <C>C.String[] strs = new String{“a””b” ”c”};</C>
   <D>D.String strs[] = newString[]{“a”, “b”, “c”};</D>
   <anwser>D</anwser>
 </question>
 <question>
    <titleid="5">执行下面的命令: java Test Red Green Blue ,请问baz的值是();</title>
   <A>A. baz has value of"Red"</A>
   <B>B. baz has value of"Blue"</B>
   <C>C. baz has value of"Green"</C>
   <D>D.代码不执行</D>
   <anwser>D</anwser>
 </question>
</questions>

原创粉丝点击