Java导出List集合到txt文件中——(四)

有时候,需要将数据以一定格式导出到txt文件中。利用Java的IO可以轻松的导出数据到txt中。

 1 package Action.txt;
 2 
 3 import java.io.BufferedWriter;
 4 import java.io.File;
 5 import java.io.FileOutputStream;
 6 import java.io.OutputStreamWriter;
 7 import java.io.Writer;
 8 import java.util.ArrayList;
 9 import java.util.List;
10 
11 import org.junit.Test;
12 
13 import bean.Question;
14 
15 /*[题干]防抱死制动系统(ABS)在什么情况下可以最大限度发挥制动器效能?
16 [类型]单选
17 [选项]{A:间歇制动},{B:持续制动},{C:紧急制动},{D:缓踏制动踏板}
18 [答案]C
19 [解析]ABS的目的就是为了防止刹车的时候一脚踩死,导致翻车什么的。 不过刹车刹死就比较慢(速度N快的时候),效果等于踩下刹车,再松下刹车,反复几次!*/
20 public class ExportTxtPaper {
21 
22     private static File outFile = new File("test.txt");
23 
24     /**
25      * 产生单选模板
26      * 
27      * @param num
28      *            单选数量
29      */
30     public static void  exportTxtPaper(List<Question> list) {
31 
32         Writer out;
33         try {
34             out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile,true), "utf-8"), 10240);
35             for (int i = 0; i < list.size(); i++) {
36                 out.write(
37                         "[题干]"+list.get(i).getTimu()+
38                         "\r\n[类型]"+list.get(i).getLeixing()
39                         + "\r\n[A选项]"+list.get(i).getAxuanxiang()
40                         + "\r\n[B选项]"+list.get(i).getBxuanxiang()
41                         + "\r\n[C选项]"+list.get(i).getCxuanxiang()
42                         + "\r\n[D选项]"+list.get(i).getDxuanxiang()
43                         + "\r\n[答案]"+list.get(i).getDaan()
44                         + "\r\n[解析]"+list.get(i).getJiexi()+"\r\n");
45                 out.write("\r\n");
46             }
47             out.flush();
48             out.close();
49         } catch (Exception e1) {
50             // TODO Auto-generated catch block
51             e1.printStackTrace();
52         }
53     }
54 
55     
56     /**
57      * 测试产生模板
58      */
59     @Test
60     public void test1(){
61         
62         List<Question> list = new ArrayList<>();
63         list.add(new Question("你喜欢吃什么", "单选", "老司机", "方便吗", "辣条", "牛奶", "老司机", "我是老司机"));
64         list.add(new Question("你喜欢吃什么", "单选", "老司机", "方便吗", "辣条", "牛奶", "老司机", "我是老司机"));
65         list.add(new Question("你喜欢吃什么", "单选", "老司机", "方便吗", "辣条", "牛奶", "老司机", "我是老司机"));
66         list.add(new Question("你喜欢吃什么", "单选", "老司机", "方便吗", "辣条", "牛奶", "老司机", "我是老司机"));
67         list.add(new Question("你是人", "判断", "", "", "", "", "是", "我是老司机"));
68         list.add(new Question("你是人", "判断", "", "", "", "", "是", "我是老司机"));
69         ExportTxtPaper .exportTxtPaper(list);
70     }
71 }

 

 

 

 结果:

 

posted @ 2017-08-02 20:20  QiaoZhi  阅读(6664)  评论(0编辑  收藏  举报