java生成TXT

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

 1 var ovr_parameters={
 2     "changeMainVideoList": [
 3     ],
 4     "indexList": [
 5         {indexName:"知识要点一1",indexTime:"3"},
 6         {indexName:"知识要点二2",indexTime:"30"},
 7         {indexName:"知识要点三3",indexTime:"80"},
 8         {indexName:"知识要点四4",indexTime:"100"},
 9         {indexName:"知识要点五5",indexTime:"150"}
10     ],
11     "importantList":[
12     {importantName:"重点1",importantTime:"50"},
13         {importantName:"重点2",importantTime:"90"},
14         {importantName:"重点3",importantTime:"103"},
15         {importantName:"重点4",importantTime:"130"},
16         {importantName:"重点5",importantTime:"250"} 
17     ],
18     "introduction": "测试课程123",
19     "rtmpURL": "0",
20     "speakor": "张三123",
21     "theme": "231国际形势与中国发展国际形势与中国发展国际形势与中国发展国际形势与中国发展国际形势与中国发展国际形势与中国发展国际形势与中国发展国际形势与中国发展国际形势与中国发展国际形势与中国发展",
22     "videoList": [
23         {
24             "fileChunkList": [
25                 {
26                     "duration": 300,
27                     "fileChunkName": "1.mp4"
28                 } 
29             ],
30             "videoFileName": ""
31         } ,
32     {
33             "fileChunkList": [
34                 {
35                     "duration": 300,
36                     "fileChunkName": "1.mp4"
37                 } 
38             ],
39             "videoFileName": ""
40         },
41 
42     {
43             "fileChunkList": [
44                 {
45                     "duration":300,
46                     "fileChunkName": "1.mp4"
47                 }
48             ],
49             "videoFileName": ""
50         }
51 
52     ]
53 }
View Code

2.生成txt方法:

 1 public void creatXML(String path, String[] names, String[] timelengths)
 2             throws IOException {  
 3         path = path + "\assets\";
 4         Document doc = DocumentHelper.createDocument();
 5         // 增加根节点
 6         Element root = doc.addElement("root");
 7         // 增加子元素
 8         Element scorm = root.addElement("Scorm");
 9         // 为子节点添加属性
10         scorm.addAttribute("theme", "");
11         scorm.addAttribute("speakor", "");
12         scorm.addAttribute("introduction", "");
13         scorm.addAttribute("ScromType", "1");
14         for (int i = 0; i < names.length; i++) {
15             String name = names[i].substring(names[i].lastIndexOf("\") + 1,
16                     names[i].length());
17             Element videoList = scorm.addElement("VideoList");
18             videoList.addAttribute("Name", "通道" + (i + 1));
19             videoList.addAttribute("Count", "1");
20             Element video = videoList.addElement("Video");
21             video.addAttribute("File", name);
22             video.addAttribute("Time", timelengths[i]);
23         }
24         Element indexList = scorm.addElement("IndexList");
25         indexList.addAttribute("Count", "0");
26         Element changeList = scorm.addElement("ChangeList");
27         changeList.addAttribute("Count", "0");
28         // 实例化输出格式对象
29         OutputFormat format = OutputFormat.createPrettyPrint();
30         // 设置输出编码
31         format.setEncoding("UTF-8");
32         // 创建需要写入的File对象
33         File file = new File(path + File.separator + "videoList.xml");
34         // 生成XMLWriter对象,构造函数中的参数为需要输出的文件流和格式
35         XMLWriter writer = new XMLWriter(new FileOutputStream(file), format);
36         // 开始写入,write方法中包含上面创建的Document对象
37         writer.write(doc);
38     }
View Code

3.调用:

1 String[] names = {"1通道","2通道","3通道"};
2 String[] timelengths = {"500","500","500"};
3 String path = "E:\video\";
4 creatXML(path,names,timelengths);
View Code
原文地址:https://www.cnblogs.com/pengpengzhang/p/7262697.html