1. package com.ufinity.mealsordering.common.util;
2.
3. import java.io.FileNotFoundException;
4. import java.io.FileOutputStream;
5. import java.io.IOException;
6.
7. import com.ufinity.mealsordering.common.Config;
8. import com.ufinity.mealsordering.common.Constant;
9. import com.ufinity.mealsordering.dao.impl.BaseDAO;
10.
11. public class CreateCSVFile extends BaseDAO{
12.
13. private static CreateCSVFile _createCSVFile = null;
14. private FileOutputStream _fos = null;
15. private StringBuffer _sBuffer = null;
16. private String _path = "";
17.
18. public static final String DEL_CHAR = ",";
19. public static final String AV_CHAR = "\"";
20.
21. public CreateCSVFile() {
22. _path = Config.getString("csv_file_path");
23. this.init(_path);
24. }
25.
26. /**
27. *
28. * @param path create csv file's path
29. */
30. public void init(String path) {
31. String method = "createCSVFile_init";
32.
33. if(path == null || path.trim().equals("")) {
34. _path = Constant.DEFAULT_PATH;
35. } else {
36. _path = path;
37. }
38. try {
39. _fos = new FileOutputStream(_path,false);
40. _sBuffer = new StringBuffer();
41. debug("", method, "create csv file sccuessful");
42. } catch (FileNotFoundException e) {
43. e.printStackTrace();
44. debug("", method, "not found exception of create csv file");
45. }
46. }
47.
48. /**
49. * this method is append date in csv file
50. * @param data
51. */
52. public void setData(Object data) {
53. _sBuffer.append(AV_CHAR);
54. _sBuffer.append(data);
55. _sBuffer.append(AV_CHAR);
56. _sBuffer.append(DEL_CHAR);
57. }
58.
59. public void writeLine() {
60. if (_sBuffer.charAt(_sBuffer.length() - 1) == ',')
61. _sBuffer.delete(_sBuffer.length() - 1, _sBuffer.length());
62. _sBuffer.append("\r\n");
63. }
64.
65. /**
66. * this method is close fileoutputstream
67. */
68. public void close() {
69. String method = "close";
70. try {
71. if(_sBuffer != null) {
72. _fos.write(_sBuffer.toString().getBytes());
73. debug("", method, "close fileOutputStream successful");
74. } else {
75. debug("", method, "null point exception");
76. }
77. } catch (IOException e) {
78. debug("", method, "close fileOutputStream failure");
79. } finally {
80. try {
81. if(_fos != null) {
82. _fos.close();
83. _fos = null;
84. }
85. } catch (IOException e) {
86. e.printStackTrace();
87. }
88. }
89. }
90. }