java生成TXT

1.由于公司需要生成如下格式TXT:

 var ovr_parameters={
"changeMainVideoList": [
],
"indexList": [
{indexName:"知识要点一1",indexTime:"3"},
{indexName:"知识要点二2",indexTime:"30"},
{indexName:"知识要点三3",indexTime:"80"},
{indexName:"知识要点四4",indexTime:"100"},
{indexName:"知识要点五5",indexTime:"150"}
],
"importantList":[
{importantName:"重点1",importantTime:"50"},
{importantName:"重点2",importantTime:"90"},
{importantName:"重点3",importantTime:"103"},
{importantName:"重点4",importantTime:"130"},
{importantName:"重点5",importantTime:"250"}
],
"introduction": "测试课程123",
"rtmpURL": "0",
"speakor": "张三123",
"theme": "231国际形势与中国发展国际形势与中国发展国际形势与中国发展国际形势与中国发展国际形势与中国发展国际形势与中国发展国际形势与中国发展国际形势与中国发展国际形势与中国发展国际形势与中国发展",
"videoList": [
{
"fileChunkList": [
{
"duration": 300,
"fileChunkName": "1.mp4"
}
],
"videoFileName": ""
} ,
{
"fileChunkList": [
{
"duration": 300,
"fileChunkName": "1.mp4"
}
],
"videoFileName": ""
}, {
"fileChunkList": [
{
"duration":300,
"fileChunkName": "1.mp4"
}
],
"videoFileName": ""
} ]
}

2.生成txt方法:

 public void creatXML(String path, String[] names, String[] timelengths)
throws IOException {
path = path + "\\assets\\";
Document doc = DocumentHelper.createDocument();
// 增加根节点
Element root = doc.addElement("root");
// 增加子元素
Element scorm = root.addElement("Scorm");
// 为子节点添加属性
scorm.addAttribute("theme", "");
scorm.addAttribute("speakor", "");
scorm.addAttribute("introduction", "");
scorm.addAttribute("ScromType", "1");
for (int i = 0; i < names.length; i++) {
String name = names[i].substring(names[i].lastIndexOf("\\") + 1,
names[i].length());
Element videoList = scorm.addElement("VideoList");
videoList.addAttribute("Name", "通道" + (i + 1));
videoList.addAttribute("Count", "1");
Element video = videoList.addElement("Video");
video.addAttribute("File", name);
video.addAttribute("Time", timelengths[i]);
}
Element indexList = scorm.addElement("IndexList");
indexList.addAttribute("Count", "0");
Element changeList = scorm.addElement("ChangeList");
changeList.addAttribute("Count", "0");
// 实例化输出格式对象
OutputFormat format = OutputFormat.createPrettyPrint();
// 设置输出编码
format.setEncoding("UTF-8");
// 创建需要写入的File对象
File file = new File(path + File.separator + "videoList.xml");
// 生成XMLWriter对象,构造函数中的参数为需要输出的文件流和格式
XMLWriter writer = new XMLWriter(new FileOutputStream(file), format);
// 开始写入,write方法中包含上面创建的Document对象
writer.write(doc);
}

3.调用:

 String[] names = {"1通道","2通道","3通道"};
String[] timelengths = {"500","500","500"};
String path = "E:\\video\\";
creatXML(path,names,timelengths);
上一篇:LVS负载均衡基础介绍及NET、DR模式配置


下一篇:foreach的指针问题