java读取csv文件并将其转成json

http://blog.csdn.net/wb453178064/article/details/53689701

分类:

  1. public class CsvUtil {  
  2.     private String fileName = null;  
  3.     private BufferedReader br = null;  
  4.     private List<String> list = new ArrayList<String>();  
  5.   
  6.     public CsvUtil() {  
  7.   
  8.     }  
  9.   
  10.     public CsvUtil(String fileName) throws Exception {  
  11.             this.fileName = fileName;  
  12.             br = new BufferedReader(new FileReader(fileName));  
  13.             String stemp;  
  14.             while ((stemp = br.readLine()) != null) {  
  15.                     list.add(stemp);  
  16.             }  
  17.     }  
  18.   
  19.     public List getList() {  
  20.             return list;  
  21.     }  
  22.     /** 
  23.      * 获取行数 
  24.      * @return 
  25.      */  
  26.     public int getRowNum() {  
  27.             return list.size();  
  28.     }  
  29.     /** 
  30.      * 获取列数 
  31.      * @return 
  32.      */  
  33.     public int getColNum() {  
  34.             if (!list.toString().equals("[]")) {  
  35.                     if (list.get(0).toString().contains(",")) {// csv为逗号分隔文件  
  36.                             return list.get(0).toString().split(",").length;  
  37.                     } else if (list.get(0).toString().trim().length() != 0) {  
  38.                             return 1;  
  39.                     } else {  
  40.                             return 0;  
  41.                     }  
  42.             } else {  
  43.                     return 0;  
  44.             }  
  45.     }  
  46.     /** 
  47.      * 获取制定行 
  48.      * @param index 
  49.      * @return 
  50.      */  
  51.     public String getRow(int index) {  
  52.             if (this.list.size() != 0) {  
  53.                     return (String) list.get(index);  
  54.             } else {  
  55.                     return null;  
  56.             }  
  57.     }  
  58.     /** 
  59.      * 获取指定列 
  60.      * @param index 
  61.      * @return 
  62.      */  
  63.     public String getCol(int index) {  
  64.             if (this.getColNum() == 0) {  
  65.                     return null;  
  66.             }  
  67.             StringBuffer sb = new StringBuffer();  
  68.             String tmp = null;  
  69.             int colnum = this.getColNum();  
  70.             if (colnum > 1) {  
  71.                     for (Iterator it = list.iterator(); it.hasNext();) {  
  72.                             tmp = it.next().toString();  
  73.                             sb = sb.append(tmp.split(",")[index] + ",");  
  74.                     }  
  75.             } else {  
  76.                     for (Iterator it = list.iterator(); it.hasNext();) {  
  77.                             tmp = it.next().toString();  
  78.                             sb = sb.append(tmp + ",");  
  79.                     }  
  80.             }  
  81.             String str = new String(sb.toString());  
  82.             str = str.substring(0, str.length() - 1);  
  83.             return str;  
  84.     }  
  85.     /** 
  86.      * 获取某个单元格 
  87.      * @param row 
  88.      * @param col 
  89.      * @return 
  90.      */  
  91.     public String getString(int row, int col) {  
  92.             String temp = null;  
  93.             int colnum = this.getColNum();  
  94.             if (colnum > 1) {  
  95.                     temp = list.get(row).toString().split(",")[col];  
  96.             } else if(colnum == 1){  
  97.                     temp = list.get(row).toString();  
  98.             } else {  
  99.                     temp = null;  
  100.             }  
  101.             return temp;  
  102.     }  
  103.   
  104.     public void CsvClose()throws Exception{  
  105.             this.br.close();  
  106.     }  
  107.     /** 
  108.     *去表头 
  109.     **/  
  110.     public String removehead(String str){  
  111.         String[] str_1=str.split(",");  
  112.         String sb=new String();  
  113.         for(int i=1;i<str_1.length;i++){  
  114.             sb=sb+str_1[i]+",";  
  115.         }  
  116.         return sb;  
  117.     }  
  118. }  
public class CsvUtil {
	private String fileName = null;
    private BufferedReader br = null;
    private List<String> list = new ArrayList<String>();

    public CsvUtil() {

    }

    public CsvUtil(String fileName) throws Exception {
            this.fileName = fileName;
            br = new BufferedReader(new FileReader(fileName));
            String stemp;
            while ((stemp = br.readLine()) != null) {
                    list.add(stemp);
            }
    }

    public List getList() {
            return list;
    }
    /**
     * 获取行数
     * @return
     */
    public int getRowNum() {
            return list.size();
    }
    /**
     * 获取列数
     * @return
     */
    public int getColNum() {
            if (!list.toString().equals("[]")) {
                    if (list.get(0).toString().contains(",")) {// csv为逗号分隔文件
                            return list.get(0).toString().split(",").length;
                    } else if (list.get(0).toString().trim().length() != 0) {
                            return 1;
                    } else {
                            return 0;
                    }
            } else {
                    return 0;
            }
    }
    /**
     * 获取制定行
     * @param index
     * @return
     */
    public String getRow(int index) {
            if (this.list.size() != 0) {
                    return (String) list.get(index);
            } else {
                    return null;
            }
    }
    /**
     * 获取指定列
     * @param index
     * @return
     */
    public String getCol(int index) {
            if (this.getColNum() == 0) {
                    return null;
            }
            StringBuffer sb = new StringBuffer();
            String tmp = null;
            int colnum = this.getColNum();
            if (colnum > 1) {
                    for (Iterator it = list.iterator(); it.hasNext();) {
                            tmp = it.next().toString();
                            sb = sb.append(tmp.split(",")[index] + ",");
                    }
            } else {
                    for (Iterator it = list.iterator(); it.hasNext();) {
                            tmp = it.next().toString();
                            sb = sb.append(tmp + ",");
                    }
            }
            String str = new String(sb.toString());
            str = str.substring(0, str.length() - 1);
            return str;
    }
    /**
     * 获取某个单元格
     * @param row
     * @param col
     * @return
     */
    public String getString(int row, int col) {
            String temp = null;
            int colnum = this.getColNum();
            if (colnum > 1) {
                    temp = list.get(row).toString().split(",")[col];
            } else if(colnum == 1){
                    temp = list.get(row).toString();
            } else {
                    temp = null;
            }
            return temp;
    }

    public void CsvClose()throws Exception{
            this.br.close();
    }
    /**
    *去表头
    **/
    public String removehead(String str){
		String[] str_1=str.split(",");
		String sb=new String();
		for(int i=1;i<str_1.length;i++){
			sb=sb+str_1[i]+",";
		}
		return sb;
    }
}
  1. public JSONArray readcsv(String path){  
  2.         JSONArray array=new JSONArray();  
  3.           
  4.             CsvUtil util;  
  5.             try {  
  6.                 util = new CsvUtil(path);  
  7.                 int row=util.getRowNum();  
  8.                 int col=util.getColNum();  
  9.                 for(int i=0;i<col;i++){  
  10.                     JSONObject jsonobject=new JSONObject();  
  11.                     String value=util.getCol(i);  
  12.                     jsonobject.put(util.getString(0, i),util.removehead(value));  
  13.                     array.add(jsonobject);  
  14.                 }  
  15.             } catch (Exception e) {  
  16.                 // TODO Auto-generated catch block  
  17.                 e.printStackTrace();  
  18.             }  
  19.         return array;  
  20.     }  
public JSONArray readcsv(String path){
		JSONArray array=new JSONArray();
		
			CsvUtil util;
			try {
				util = new CsvUtil(path);
				int row=util.getRowNum();
				int col=util.getColNum();
				for(int i=0;i<col;i++){
					JSONObject jsonobject=new JSONObject();
					String value=util.getCol(i);
					jsonobject.put(util.getString(0, i),util.removehead(value));
					array.add(jsonobject);
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		return array;
	}

将其表头设为key 其他内容设为value

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值