读取txt文件转换成map,以及将map…
来源:互联网 发布:iphone通讯录恢复软件 编辑:程序博客网 时间:2024/05/02 00:19
- 第一段代码是我从一个文件读出数据,并且将数据用#分割开来
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);
//定义输出格式:创建一个合适的输出格式
} 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>
</questions>
- 读取txt文件转换成map,以及将map…
- clojure读取文件->转换Map
- java中读取properties文件,并将文件内容转换成map并返回
- 将Map转换成Class
- io流将txt文件转成map集合的形式
- java将XML数据转换成Map
- 如何将Map转换成Gson json
- gson ,将json转换成Map
- Jackson 将Map对象转换成JSON
- map.txt
- 将Txt文件转换成dataset
- 将实体转换为map
- python读取 .txt 文本内容以及将程序执行结果写入txt文件
- 将xml字符串转换成map,list 的转换
- java按行读取txt文件并按顺序放到map对象里面实例
- 从Word中读取内容&将word转换成txt
- java方式的将java对象以及list或者map转化为json数据,json转换为object或者map
- 将txt文件转换成excel文件的方法
- 饭店点餐系统之点菜流程
- 饭店点餐系统之数据库表结构
- C#中构造函数和析构函数的用法
- 在eclipse或者myeclipse下根据.dtd…
- xpath的使用,希望能帮到你
- 读取txt文件转换成map,以及将map…
- Struts配置数据源
- Android量产软件客制化
- html 的初识
- android在信息栏显示推送信息,并…
- 在windows8 metro中计算TextBox的实际宽度ActualWidth
- getAttribute和getParameter区别
- 创业软件公司的18个致命伤
- 让DB2跑得更快——DB2内部解析与性能优化