码迷,mamicode.com
首页 > 编程语言 > 详细

java生成TXT

时间:2017-07-31 14:30:34      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:document   index   参数   leo   eof   bsp   int   type   output   

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

 

java生成TXT

标签:document   index   参数   leo   eof   bsp   int   type   output   

原文地址:http://www.cnblogs.com/pengpengzhang/p/7262697.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!