Upload usecase-ui server structure code
[usecase-ui/server.git] / src / main / java / org / onap / usecaseui / server / util / CSVUtils.java
1 /*
2  * Copyright (C) 2017 CMCC, Inc. and others. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onap.usecaseui.server.util;
17
18 import org.apache.commons.csv.CSVFormat;
19 import org.apache.commons.csv.CSVParser;
20 import org.apache.commons.csv.CSVPrinter;
21 import org.apache.commons.csv.CSVRecord;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 import java.io.FileReader;
26 import java.io.FileWriter;
27 import java.io.IOException;
28 import java.io.Writer;
29 import java.util.List;
30
31 public class CSVUtils {
32     //CSV文件分隔符
33     private final static String NEW_LINE_SEPARATOR="\n";
34     private static Logger logger = LoggerFactory.getLogger(CSVUtils.class);
35
36
37     /**写入csv文件
38      * @param headers 列头
39      * @param data 数据内容
40      * @param filePath 创建的csv文件路径
41      * **/
42     public static void writeCsv(String[] headers,List<String[]> data,String filePath) {
43     }
44
45     /**读取csv文件
46      * @param filePath 文件路径
47      * @param headers csv列头
48      * @return CSVRecord 列表
49      * @throws IOException **/
50     public static List<CSVRecord> readCSV(String filePath, String[] headers) throws IOException{
51         //创建CSVFormat
52         CSVFormat formator = CSVFormat.DEFAULT.withHeader(headers);
53         FileReader fileReader=new FileReader(filePath);
54         //创建CSVParser对象
55         CSVParser parser=new CSVParser(fileReader,formator);
56         List<CSVRecord> records=parser.getRecords();
57         parser.close();
58         fileReader.close();
59         return records;
60     }
61
62 }