列表

详情


87. 怎么把csv文件导入java中,怎么处理数据,存储成什么格式?

回答思路

概念:

csv是逗号分隔文件(comma separated values),是一种用来存储数据的纯文本格式,通常用于电子表格或数据库软件。在csv文件中,数据“栏”以逗号分隔,可允许程序通过读取文件为数据重新创建正确的栏结构,并在每次遇到逗号时开始新的一栏。

csv导入/导出java接口代码:

TypeScript:

package com.parami.utils; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.List; public class CSVUtils {     /**      *导出       * @param file csv文件(路径+文件名),csv文件不存在会自动创建      * @param dataList数据      * @return      */     public static boolean exportCsv(File file, List<string> dataList){         boolean isSucess=false;         FileOutputStream out=null;         OutputStreamWriter osw=null;         BufferedWriter bw=null;         try {             out = new FileOutputStream(file);             osw = new OutputStreamWriter(out);             bw =new BufferedWriter(osw);             if(dataList!=null && !dataList.isEmpty()){                 for(String data : dataList){                     bw.append(data).append("\r");                 }             }             isSucess=true;         } catch (Exception e) {             isSucess=false;         }finally{             if(bw!=null){                 try {                     bw.close();                     bw=null;                 } catch (IOException e) {                     e.printStackTrace();                 }              }             if(osw!=null){                 try {                     osw.close();                     osw=null;                 } catch (IOException e) {                     e.printStackTrace();                 }              }             if(out!=null){                 try {                     out.close();                     out=null;                 } catch (IOException e) {                     e.printStackTrace();                 }              }         }          return isSucess;     }      /**      *导入       * @param file csv文件(路径+文件)      * @return      */     public static List<string> importCsv(File file){         List<string> dataList=new ArrayList<string>();          BufferedReader br=null;         try {              br = new BufferedReader(new FileReader(file));             String line = "";              while ((line = br.readLine()) != null) {                  dataList.add(line);             }         }catch (Exception e) {         }finally{             if(br!=null){                 try {                     br.close();                     br=null;                 } catch (IOException e) {                     e.printStackTrace();                 }             }         }         return dataList;     } } </string></string></string></string>

csv文件导入导出单测代码:

TypeScript:

package junit.test; import java.io.File; import java.util.ArrayList; import java.util.List; import org.junit.Test; import com.parami.utils.CSVUtils; public class CsvTest { /** * CSV导出* @throws Exception */ public void exportCsv() { List<string> dataList=new ArrayList<string>(); dataList.add("1,张三,男"); dataList.add("2,李四,男"); dataList.add("3,小红,女"); boolean isSuccess=CSVUtils.exportCsv(new File("D:/test/lidian.csv"), dataList); System.out.println(isSuccess); } /** * CSV导出* @throws Exception */ public void importCsv() { List<string> dataList=CSVUtils.importCsv(new File("D:/test/lidian.csv")); if(dataList!=null && !dataList.isEmpty()){ for(String data : dataList){ System.out.println(data); } } } } </string></string></string>

上一题