2 * Copyright (C) 2017 CMCC, Inc. and others. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onap.usecaseui.server.util;
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;
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;
31 public class CSVUtils {
33 private final static String NEW_LINE_SEPARATOR="\n";
34 private static Logger logger = LoggerFactory.getLogger(CSVUtils.class);
40 * @param filePath 创建的csv文件路径
42 public static void writeCsv(String[] headers,List<String[]> data,String filePath) {
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{
52 CSVFormat formator = CSVFormat.DEFAULT.withHeader(headers);
53 FileReader fileReader=new FileReader(filePath);
55 CSVParser parser=new CSVParser(fileReader,formator);
56 List<CSVRecord> records=parser.getRecords();