CSVWriter生成文件時writer.writeRecord();方法保存的文件末尾多一個空行


一.問題,CSVWriter生成文件時使用writer.writeRecord();方法保存的文件末尾多一個空行,效果圖如下:

目標結果:(去掉末尾空行)

二.關鍵代碼如下(修改前代碼):

 1 /**
 2      * 生成CSV文件
 3      * @param filePath 文件保存路徑,例如:D:/temp/test.csv
 4      * @param headerBeans 實體對象集合
 5      * @param detailBeans 實體對象集合
 6      * @param trailerBeans 實體對象集合
 7      * @param <T>
 8      */
 9     public static <T> void createFile(String filePath, List<T> headerBeans, List<T> detailBeans, List<T> trailerBeans) {
10         CsvWriter writer = null;
11         try {
12             // 創建文件對象
13             File file = createFile(filePath);
14             // 生成文件
15             writer = new CsvWriter(filePath, ',', Charset.forName("GBK"));
16             // 獲取內容
17             List<String[]> contents = new ArrayList<>();
18             List<String[]> headerContents = getStringArrayFromBean(headerBeans);
19             List<String[]> detailContents = getStringArrayFromBean(detailBeans);
20             List<String[]> trailerContents = getStringArrayFromBean(trailerBeans);
21             contents.addAll(headerContents);
22             contents.addAll(detailContents);
23             contents.addAll(trailerContents);
24             // 寫入內容
25             for (String[] each : contents) {
26                 writer.writeRecord(each);
27             }
28         } catch (Exception e) {
29             LOGGER.error("生成CSV文件失敗", e);
30         } finally {
31             if (writer != null) {
32                 writer.close();
33             }
34         }
35     }

輸出文件就在第26行,writer.writeRecord(each);點進去跳到import com.csvreader.CsvWriter;這個包中,如下截圖(序號為程序執行調用順序):

 

可以看到執行到endRecord()方法時,總是會執行else中的內容,因為useCustomRecordDeliniter定義的為false...(就是這導致了末尾多一空行)

三.解決問題:

.class文件又不能改, 在這里是換了一種寫法 ( 由原來的CSVWriter的writeRecord() 改為用PrintWriter的pw.write() )

 1 /**
 2      * 生成CSV文件
 3      * @param filePath 文件保存路徑,例如:D:/temp/test.csv
 4      * @param headerBeans 實體對象集合
 5      * @param detailBeans 實體對象集合
 6      * @param trailerBeans 實體對象集合
 7      * @param <T>
 8      */
 9     public static <T> void createFile(String filePath, List<T> headerBeans, List<T> detailBeans, List<T> trailerBeans) {
10         CsvWriter writer = null;
11         try {
12             // 創建文件對象
13             File file = createFile(filePath);
14             // 生成文件
15             writer = new CsvWriter(filePath, ',', Charset.forName("GBK"));
16             // 獲取內容
17             List<String[]> contents = new ArrayList<>();
18             List<String[]> headerContents = getStringArrayFromBean(headerBeans);
19             List<String[]> detailContents = getStringArrayFromBean(detailBeans);
20             List<String[]> trailerContents = getStringArrayFromBean(trailerBeans);
21             contents.addAll(headerContents);
22             contents.addAll(detailContents);
23             contents.addAll(trailerContents);
24             // 重組內容
25             String result = "";
26             for (String[] each : contents) {
27                 for (String s : each){
28                     writer.writeRecord(each);
29                     result += s ;
30                     result += ",";
31                 }
32                 result = result.substring(0,result.length()-1);
33                 result += "\r\n";
34             }
35             result = result.substring(0,result.length()-2);
36 
37             writeFileContent(filePath,result);//寫入
38         } catch (Exception e) {
39             LOGGER.error("生成CSV文件失敗", e);
40         } finally {
41             if (writer != null) {
42                 writer.close();
43             }
44         }
45     }

調用的writeFileContent()方法如下:

/**
     * 向文件中寫入內容
     *
     * @param filepath 文件路徑與名稱
     * @param newstr   寫入的內容
     * @return
     * @throws IOException
     */
    public static boolean writeFileContent(String filepath, String newstr) throws IOException {
        Boolean bool = false;
        String temp = "";
        FileInputStream fis = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        FileOutputStream fos = null;
        PrintWriter pw = null;
        try {
           /* File file = new File(filepath);*///文件路徑(包括文件名稱)
            //將文件讀入輸入流
            fis = new FileInputStream(filepath);
            isr = new InputStreamReader(fis);
            br = new BufferedReader(isr);

//            StringBuffer buffer = new StringBuffer();
//            //文件原有內容
//            for (int i = 0; (temp = br.readLine()) != null; i++) {
//                buffer.append(temp);
//                // 行與行之間的分隔符 相當於“\n”
//                buffer = buffer.append(System.getProperty("line.separator"));
//            }
//            buffer.append(newstr);

            fos = new FileOutputStream(filepath);
            pw = new PrintWriter(fos);
            pw.write(newstr.toCharArray());
            pw.flush();
            bool = true;
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            throw e;
        } finally {
            //不要忘記關閉
            if (pw != null) {
                pw.close();
            }
            if (fos != null) {
                fos.close();
            }
            if (br != null) {
                br.close();
            }
            if (isr != null) {
                isr.close();
            }
            if (fis != null) {
                fis.close();
            }
        }
        return bool;
    }

 

貼上完整的新utils留個備份:

  1 package com.sp.ppms.console.finance.CSV;
  2 
  3 import java.io.*;
  4 import java.lang.reflect.Field;
  5 import java.lang.reflect.Method;
  6 import java.nio.charset.Charset;
  7 import java.util.ArrayList;
  8 import java.util.Collections;
  9 import java.util.HashMap;
 10 import java.util.List;
 11 import java.util.Map;
 12 import java.util.Map.Entry;
 13 import org.apache.commons.lang3.StringUtils;
 14 import org.apache.log4j.Logger;
 15 import org.springframework.util.ReflectionUtils;
 16 import com.csvreader.CsvReader;
 17 import com.csvreader.CsvWriter;
 18 
 19 /**
 20  * CSV工具類
 21  */
 22 public class CSVUtil {
 23     /**
 24      * 日志對象
 25      **/
 26     private static final Logger LOGGER = Logger.getLogger(CSVUtil.class);
 27 
 28     /**
 29      * 生成CSV文件
 30      * @param filePath 文件保存路徑,例如:D:/temp/test.csv
 31      * @param headerBeans 實體對象集合
 32      * @param detailBeans 實體對象集合
 33      * @param trailerBeans 實體對象集合
 34      * @param <T>
 35      */
 36     public static <T> void createFile(String filePath, List<T> headerBeans, List<T> detailBeans, List<T> trailerBeans) {
 37         CsvWriter writer = null;
 38         try {
 39             // 創建文件對象
 40             File file = createFile(filePath);
 41             // 生成文件
 42             writer = new CsvWriter(filePath, ',', Charset.forName("GBK"));
 43             // 獲取內容
 44             List<String[]> contents = new ArrayList<>();
 45             List<String[]> headerContents = getStringArrayFromBean(headerBeans);
 46             List<String[]> detailContents = getStringArrayFromBean(detailBeans);
 47             List<String[]> trailerContents = getStringArrayFromBean(trailerBeans);
 48             contents.addAll(headerContents);
 49             contents.addAll(detailContents);
 50             contents.addAll(trailerContents);
 51             // 重組內容
 52             String result = "";
 53             for (String[] each : contents) {
 54                 for (String s : each){
 55                     result += s ;
 56                     result += ",";
 57                 }
 58                 result = result.substring(0,result.length()-1);
 59                 result += "\r\n";
 60             }
 61             result = result.substring(0,result.length()-2);
 62 
 63             writeFileContent(filePath,result);//寫入
 64         } catch (Exception e) {
 65             LOGGER.error("生成CSV文件失敗", e);
 66         } finally {
 67             if (writer != null) {
 68                 writer.close();
 69             }
 70         }
 71     }
 72 
 73     /**
 74      * 向文件中寫入內容
 75      *
 76      * @param filepath 文件路徑與名稱
 77      * @param newstr   寫入的內容
 78      * @return
 79      * @throws IOException
 80      */
 81     public static boolean writeFileContent(String filepath, String newstr) throws IOException {
 82         Boolean bool = false;
 83         String temp = "";
 84         FileInputStream fis = null;
 85         InputStreamReader isr = null;
 86         BufferedReader br = null;
 87         FileOutputStream fos = null;
 88         PrintWriter pw = null;
 89         try {
 90            /* File file = new File(filepath);*///文件路徑(包括文件名稱)
 91             //將文件讀入輸入流
 92             fis = new FileInputStream(filepath);
 93             isr = new InputStreamReader(fis);
 94             br = new BufferedReader(isr);
 95 
 96 //            StringBuffer buffer = new StringBuffer();
 97 //            //文件原有內容
 98 //            for (int i = 0; (temp = br.readLine()) != null; i++) {
 99 //                buffer.append(temp);
100 //                // 行與行之間的分隔符 相當於“\n”
101 //                buffer = buffer.append(System.getProperty("line.separator"));
102 //            }
103 //            buffer.append(newstr);
104 
105             fos = new FileOutputStream(filepath);
106             pw = new PrintWriter(fos);
107             pw.write(newstr.toCharArray());
108             pw.flush();
109             bool = true;
110         } catch (Exception e) {
111             // TODO: handle exception
112             e.printStackTrace();
113             throw e;
114         } finally {
115             //不要忘記關閉
116             if (pw != null) {
117                 pw.close();
118             }
119             if (fos != null) {
120                 fos.close();
121             }
122             if (br != null) {
123                 br.close();
124             }
125             if (isr != null) {
126                 isr.close();
127             }
128             if (fis != null) {
129                 fis.close();
130             }
131         }
132         return bool;
133     }
134 
135     /**
136      * 讀取CSV文件內容
137      *
138      * @param filePath 文件存放的路徑,如:D:/csv/xxx.csv
139      * @param bean     類類型
140      * @return List<T>
141      */
142     public static <T> List<T> readFile(String filePath, Class<T> bean) {
143         List<String[]> dataList = new ArrayList<String[]>();
144         CsvReader reader = null;
145         try {
146             // 創建CSV讀對象 例如:CsvReader(文件路徑,分隔符,編碼格式);
147             reader = new CsvReader(filePath, ',', Charset.forName("GBK"));
148             if (reader != null) {
149                 // 跳過表頭,如果需要表頭的話,這句可以忽略
150                 //reader.readHeaders();
151                 // 逐行讀入除表頭的數據
152                 while (reader.readRecord()) {
153                     dataList.add(reader.getValues());
154                 }
155                 if (!dataList.isEmpty()) {
156                     // 數組轉對象
157                     return getBeanFromStringArray(dataList, bean);
158                 }
159             }
160         } catch (Exception e) {
161             LOGGER.error("讀取CSV文件失敗", e);
162         } finally {
163             if (reader != null) {
164                 reader.close();
165             }
166         }
167         return Collections.emptyList();
168     }
169 
170     /**
171      * 刪除該目錄下所有文件
172      *
173      * @param filePath 文件目錄路徑,如:d:/test
174      */
175     public static boolean deleteFiles(String filePath) {
176         File file = new File(filePath);
177         if (file.exists()) {
178             File[] files = file.listFiles();
179             if (files != null && files.length > 0) {
180                 for (File f : files) {
181                     if (f.isFile() && f.delete()) {
182                         LOGGER.info("刪除" + f.getName() + "文件成功");
183                     }
184                 }
185                 return true;
186             }
187         }
188         return false;
189     }
190 
191     /**
192      * 刪除單個文件
193      *
194      * @param filePath 文件目錄路徑,如:d:/test
195      * @param fileName 文件名稱,如:110.csv
196      */
197     public static boolean deleteFile(String filePath, String fileName) {
198         File file = new File(filePath);
199         if (file.exists()) {
200             File[] files = file.listFiles();
201             if (files != null && files.length > 0) {
202                 for (File f : files) {
203                     if (f.isFile() && f.getName().equals(fileName)) {
204                         return f.delete();
205                     }
206                 }
207             }
208         }
209         return false;
210     }
211 
212     /**
213      * 泛型實體轉換為數組
214      *
215      * @param beans
216      * @return List<String[]>
217      */
218     private static <T> List<String[]> getStringArrayFromBean(List<T> beans) {
219         List<String[]> result = new ArrayList<String[]>();
220         Class<? extends Object> cls = beans.get(0).getClass();
221         Field[] declaredFields = cls.getDeclaredFields();
222         List<Field> annoFields = new ArrayList<Field>();
223         // 篩選出標有注解的字段
224         for (Field field : declaredFields) {
225             CSVField anno = field.getAnnotation(CSVField.class);
226             if (anno != null) {
227                 annoFields.add(field);
228             }
229         }
230         // 獲取注解的值,即內容標題
231         String[] title = new String[annoFields.size()];
232         /*for (int i = 0; i < annoFields.size(); i++) {
233             title[i] = annoFields.get(i).getAnnotation(CSVField.class).name();
234         }
235         result.add(title);*/
236         try {
237             // 獲取內容
238             for (T t : beans) {
239                 String[] item = new String[annoFields.size()];
240                 int index = 0;
241                 for (Field field : annoFields) {
242                     String fieldName = field.getName();
243                     String methodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
244                     Method method = ReflectionUtils.findMethod(t.getClass(), methodName);
245                     if (method != null) {
246                         Object value = ReflectionUtils.invokeMethod(method, t);
247                         if (value == null) {
248                             item[index] = "";
249                         } else {
250                             item[index] = value.toString();
251                         }
252                     }
253                     index++;
254                 }
255                 result.add(item);
256             }
257         } catch (Exception e) {
258             LOGGER.info("實體對象轉數組失敗", e);
259         }
260         return result;
261     }
262 
263     /**
264      * 數組轉為對象集合
265      *
266      * @param dataList
267      * @param bean
268      * @return List<T>
269      */
270     private static <T> List<T> getBeanFromStringArray(List<String[]> dataList, Class<T> bean) {
271         List<T> list = new ArrayList<>();
272         List<Map<String, String>> titles = getTitles(dataList);
273         Map<String, Field> fields = getFields(bean);
274         try {
275             for (Map<String, String> map : titles) {
276                 T t = bean.newInstance();
277                 for (Entry<String, String> entry : map.entrySet()) {
278                     if (fields.containsKey(entry.getKey())) {
279                         Field field = fields.get(entry.getKey());
280                         Class<?> valType = field.getType();
281                         String fieldName = field.getName();
282                         String methodName = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
283                         Method method = ReflectionUtils.findMethod(bean, methodName, valType);
284                         if (method != null) {
285                             ReflectionUtils.invokeMethod(method, t, entry.getValue());
286                         }
287                     }
288                 }
289                 list.add(t);
290             }
291         } catch (Exception e) {
292             LOGGER.error("創建實體失敗", e);
293         }
294         return list;
295     }
296 
297     /**
298      * 數組標題與值的對應關系
299      *
300      * @param dataList
301      * @return
302      */
303     private static <T> List<Map<String, String>> getTitles(List<String[]> dataList) {
304         List<Map<String, String>> list = new ArrayList<>();
305         String[] titles = dataList.get(0);
306         dataList.remove(0);
307         for (String[] values : dataList) {
308             Map<String, String> titleMap = new HashMap<>();
309             for (int i = 0; i < values.length; i++) {
310                 titleMap.put(titles[i], values[i]);
311             }
312             list.add(titleMap);
313         }
314         return list;
315     }
316 
317     /**
318      * 注解名稱與字段屬性的對應關系
319      *
320      * @param clazz 實體對象類類型
321      * @param <T>   泛型類型
322      * @return Map<String , Field>
323      */
324     private static <T> Map<String, Field> getFields(Class<T> clazz) {
325         Map<String, Field> annoMap = new HashMap<>();
326         Field[] fileds = clazz.getDeclaredFields();
327         for (Field filed : fileds) {
328             CSVField anno = filed.getAnnotation(CSVField.class);
329             if (anno != null) {
330                 // 獲取name屬性值
331                 if (StringUtils.isNotBlank(anno.name())) {
332                     annoMap.put(anno.name(), filed);
333                 }
334             }
335         }
336         return annoMap;
337     }
338 
339     /**
340      * 創建文件對象
341      *
342      * @param filePath 文件路徑,例如:temp/test.csv
343      * @return File
344      */
345     private static File createFile(String filePath) {
346         File file = null;
347         try {
348             // 創建文件目錄
349             file = new File(filePath.substring(0, filePath.lastIndexOf('/')));
350             if (!file.exists()) {
351                 file.mkdirs();
352             }
353             // 創建文件對象
354             file = new File(filePath);
355             if (!file.exists() && file.createNewFile()) {
356                 LOGGER.info("創建文件對象成功");
357             }
358         } catch (IOException e) {
359             LOGGER.error("創建文件對象失敗", e);
360         }
361         return file;
362 
363     }
364 }
View Code

廢棄utils:

  1 package com.sp.ppms.console.finance.CSV;
  2 
  3 import java.io.File;
  4 import java.io.IOException;
  5 import java.lang.reflect.Field;
  6 import java.lang.reflect.Method;
  7 import java.nio.charset.Charset;
  8 import java.util.ArrayList;
  9 import java.util.Collections;
 10 import java.util.HashMap;
 11 import java.util.List;
 12 import java.util.Map;
 13 import java.util.Map.Entry;
 14 import org.apache.commons.lang3.StringUtils;
 15 import org.apache.log4j.Logger;
 16 import org.springframework.util.ReflectionUtils;
 17 import com.csvreader.CsvReader;
 18 import com.csvreader.CsvWriter;
 19 
 20 /**
 21  * CSV工具類
 22  */
 23 public class CSVUtil {
 24     /**
 25      * 日志對象
 26      **/
 27     private static final Logger LOGGER = Logger.getLogger(CSVUtil.class);
 28 
 29     /**
 30      * 生成CSV文件
 31      * @param filePath 文件保存路徑,例如:D:/temp/test.csv
 32      * @param headerBeans 實體對象集合
 33      * @param detailBeans 實體對象集合
 34      * @param trailerBeans 實體對象集合
 35      * @param <T>
 36      */
 37     public static <T> void createFile(String filePath, List<T> headerBeans, List<T> detailBeans, List<T> trailerBeans) {
 38         CsvWriter writer = null;
 39         try {
 40             // 創建文件對象
 41             File file = createFile(filePath);
 42             // 生成文件
 43             writer = new CsvWriter(filePath, ',', Charset.forName("GBK"));
 44             // 獲取內容
 45             List<String[]> contents = new ArrayList<>();
 46             List<String[]> headerContents = getStringArrayFromBean(headerBeans);
 47             List<String[]> detailContents = getStringArrayFromBean(detailBeans);
 48             List<String[]> trailerContents = getStringArrayFromBean(trailerBeans);
 49             contents.addAll(headerContents);
 50             contents.addAll(detailContents);
 51             contents.addAll(trailerContents);
 52             // 寫入內容
 53             for (String[] each : contents) {
 54                 writer.writeRecord(each);
 55             }
 56         } catch (Exception e) {
 57             LOGGER.error("生成CSV文件失敗", e);
 58         } finally {
 59             if (writer != null) {
 60                 writer.close();
 61             }
 62         }
 63     }
 64 
 65     /**
 66      * 讀取CSV文件內容
 67      *
 68      * @param filePath 文件存放的路徑,如:D:/csv/xxx.csv
 69      * @param bean     類類型
 70      * @return List<T>
 71      */
 72     public static <T> List<T> readFile(String filePath, Class<T> bean) {
 73         List<String[]> dataList = new ArrayList<String[]>();
 74         CsvReader reader = null;
 75         try {
 76             // 創建CSV讀對象 例如:CsvReader(文件路徑,分隔符,編碼格式);
 77             reader = new CsvReader(filePath, ',', Charset.forName("GBK"));
 78             if (reader != null) {
 79                 // 跳過表頭,如果需要表頭的話,這句可以忽略
 80                 //reader.readHeaders();
 81                 // 逐行讀入除表頭的數據
 82                 while (reader.readRecord()) {
 83                     dataList.add(reader.getValues());
 84                 }
 85                 if (!dataList.isEmpty()) {
 86                     // 數組轉對象
 87                     return getBeanFromStringArray(dataList, bean);
 88                 }
 89             }
 90         } catch (Exception e) {
 91             LOGGER.error("讀取CSV文件失敗", e);
 92         } finally {
 93             if (reader != null) {
 94                 reader.close();
 95             }
 96         }
 97         return Collections.emptyList();
 98     }
 99 
100     /**
101      * 刪除該目錄下所有文件
102      *
103      * @param filePath 文件目錄路徑,如:d:/test
104      */
105     public static boolean deleteFiles(String filePath) {
106         File file = new File(filePath);
107         if (file.exists()) {
108             File[] files = file.listFiles();
109             if (files != null && files.length > 0) {
110                 for (File f : files) {
111                     if (f.isFile() && f.delete()) {
112                         LOGGER.info("刪除" + f.getName() + "文件成功");
113                     }
114                 }
115                 return true;
116             }
117         }
118         return false;
119     }
120 
121     /**
122      * 刪除單個文件
123      *
124      * @param filePath 文件目錄路徑,如:d:/test
125      * @param fileName 文件名稱,如:110.csv
126      */
127     public static boolean deleteFile(String filePath, String fileName) {
128         File file = new File(filePath);
129         if (file.exists()) {
130             File[] files = file.listFiles();
131             if (files != null && files.length > 0) {
132                 for (File f : files) {
133                     if (f.isFile() && f.getName().equals(fileName)) {
134                         return f.delete();
135                     }
136                 }
137             }
138         }
139         return false;
140     }
141 
142     /**
143      * 泛型實體轉換為數組
144      *
145      * @param beans
146      * @return List<String[]>
147      */
148     private static <T> List<String[]> getStringArrayFromBean(List<T> beans) {
149         List<String[]> result = new ArrayList<String[]>();
150         Class<? extends Object> cls = beans.get(0).getClass();
151         Field[] declaredFields = cls.getDeclaredFields();
152         List<Field> annoFields = new ArrayList<Field>();
153         // 篩選出標有注解的字段
154         for (Field field : declaredFields) {
155             CSVField anno = field.getAnnotation(CSVField.class);
156             if (anno != null) {
157                 annoFields.add(field);
158             }
159         }
160         // 獲取注解的值,即內容標題
161         String[] title = new String[annoFields.size()];
162         /*for (int i = 0; i < annoFields.size(); i++) {
163             title[i] = annoFields.get(i).getAnnotation(CSVField.class).name();
164         }
165         result.add(title);*/
166         try {
167             // 獲取內容
168             for (T t : beans) {
169                 String[] item = new String[annoFields.size()];
170                 int index = 0;
171                 for (Field field : annoFields) {
172                     String fieldName = field.getName();
173                     String methodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
174                     Method method = ReflectionUtils.findMethod(t.getClass(), methodName);
175                     if (method != null) {
176                         Object value = ReflectionUtils.invokeMethod(method, t);
177                         if (value == null) {
178                             item[index] = "";
179                         } else {
180                             item[index] = value.toString();
181                         }
182                     }
183                     index++;
184                 }
185                 result.add(item);
186             }
187         } catch (Exception e) {
188             LOGGER.info("實體對象轉數組失敗", e);
189         }
190         return result;
191     }
192 
193     /**
194      * 數組轉為對象集合
195      *
196      * @param dataList
197      * @param bean
198      * @return List<T>
199      */
200     private static <T> List<T> getBeanFromStringArray(List<String[]> dataList, Class<T> bean) {
201         List<T> list = new ArrayList<>();
202         List<Map<String, String>> titles = getTitles(dataList);
203         Map<String, Field> fields = getFields(bean);
204         try {
205             for (Map<String, String> map : titles) {
206                 T t = bean.newInstance();
207                 for (Entry<String, String> entry : map.entrySet()) {
208                     if (fields.containsKey(entry.getKey())) {
209                         Field field = fields.get(entry.getKey());
210                         Class<?> valType = field.getType();
211                         String fieldName = field.getName();
212                         String methodName = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
213                         Method method = ReflectionUtils.findMethod(bean, methodName, valType);
214                         if (method != null) {
215                             ReflectionUtils.invokeMethod(method, t, entry.getValue());
216                         }
217                     }
218                 }
219                 list.add(t);
220             }
221         } catch (Exception e) {
222             LOGGER.error("創建實體失敗", e);
223         }
224         return list;
225     }
226 
227     /**
228      * 數組標題與值的對應關系
229      *
230      * @param dataList
231      * @return
232      */
233     private static <T> List<Map<String, String>> getTitles(List<String[]> dataList) {
234         List<Map<String, String>> list = new ArrayList<>();
235         String[] titles = dataList.get(0);
236         dataList.remove(0);
237         for (String[] values : dataList) {
238             Map<String, String> titleMap = new HashMap<>();
239             for (int i = 0; i < values.length; i++) {
240                 titleMap.put(titles[i], values[i]);
241             }
242             list.add(titleMap);
243         }
244         return list;
245     }
246 
247     /**
248      * 注解名稱與字段屬性的對應關系
249      *
250      * @param clazz 實體對象類類型
251      * @param <T>   泛型類型
252      * @return Map<String , Field>
253      */
254     private static <T> Map<String, Field> getFields(Class<T> clazz) {
255         Map<String, Field> annoMap = new HashMap<>();
256         Field[] fileds = clazz.getDeclaredFields();
257         for (Field filed : fileds) {
258             CSVField anno = filed.getAnnotation(CSVField.class);
259             if (anno != null) {
260                 // 獲取name屬性值
261                 if (StringUtils.isNotBlank(anno.name())) {
262                     annoMap.put(anno.name(), filed);
263                 }
264             }
265         }
266         return annoMap;
267     }
268 
269     /**
270      * 創建文件對象
271      *
272      * @param filePath 文件路徑,例如:temp/test.csv
273      * @return File
274      */
275     private static File createFile(String filePath) {
276         File file = null;
277         try {
278             // 創建文件目錄
279             file = new File(filePath.substring(0, filePath.lastIndexOf('/')));
280             if (!file.exists()) {
281                 file.mkdirs();
282             }
283             // 創建文件對象
284             file = new File(filePath);
285             if (!file.exists() && file.createNewFile()) {
286                 LOGGER.info("創建文件對象成功");
287             }
288         } catch (IOException e) {
289             LOGGER.error("創建文件對象失敗", e);
290         }
291         return file;
292 
293     }
294 }
View Code

 

另:

  嚴格講\n是換行,\r是回車。但不同的系統、不同的應用可能做不同的處理。在文本文件中,換行是兩者的組合"\r\n"。

 

感謝:https://bbs.csdn.net/topics/40116687.

  https://jingyan.baidu.com/article/0aa223754f1fc288cd0d6479.html

拓展:https://blog.csdn.net/pfm685757/article/details/47806469#commentBox

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM