csv读写和更改一行的记录

package com.hoperun.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.bytecode.opencsv.CSVWriter;

 

public class CsvUtil1 {
 private String filename = null;

 private BufferedReader bufferedreader = null;
 private File file;
 private List<String[]> list = new ArrayList<String[]>();

 public CsvUtil1() {

 }

 public CsvUtil1(String filename) throws IOException {
  this.filename = filename;
  file = new File(filename);
  bufferedreader = new BufferedReader(new FileReader(filename));
  String stemp;
  while ((stemp = bufferedreader.readLine()) != null) {
   String[] objs = stemp.split(",");

   list.add(objs);
  }
  bufferedreader.close();
 }

 public void setList(List list) {
  this.list = list;
 }

 public CsvUtil1(File file) throws IOException {
  this.file = file;
  this.filename = file.getName();
  bufferedreader = new BufferedReader(new FileReader(file));
  String stemp;
  while ((stemp = bufferedreader.readLine()) != null) {
   String[] objs = stemp.split(",");

   list.add(objs);
  }
  bufferedreader.close();
 }

 public List getList() throws IOException {
  return list;
 }

 // ��ȡ��¼��
 public int getRowNum() {
  return list.size();
 }

 public int getColNum() {
  if (!list.toString().equals("[]")) {
   if (list.get(0).toString().contains(",")) {
    return list.get(0).toString().split(",").length;
   } else if (list.get(0).toString().trim().length() != 0) {
    return 1;
   } else {
    return 0;
   }
  } else {
   return 0;
  }
 }

 // ��ȡһ�в�������ת�����ַ�
 public String[] getRow(int index) {
  if (this.list.size() != 0)
   return (String[]) list.get(index);
  else
   return null;
 }

 //
 public String getCol(int index) {
  if (this.getColNum() == 0) {
   return null;
  }
  StringBuffer scol = new StringBuffer();
  String temp = null;
  int colnum = this.getColNum();
  if (colnum > 1) {
   for (Iterator it = list.iterator(); it.hasNext();) {
    temp = it.next().toString();

    scol = scol.append(temp.split(",")[index] + ",");
   }
  } else {
   for (Iterator it = list.iterator(); it.hasNext();) {
    temp = it.next().toString();
    scol = scol.append(temp + ",");
   }
  }
  String str = new String(scol.toString());
  str = str.substring(0, str.length() - 1);
  return str;
 }

 // ��������ָ��λ�õ��ַ�
 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 static String removestr(String str) {
  StringBuffer sb = new StringBuffer(str);
  for (int i = 0; i < sb.length(); i++) {
   if (sb.charAt(i) == 34) {
    sb.deleteCharAt(i);
   }
  }
  return sb.toString();
 }

 public void removeStrInList() {
  for (String[] strs : list) {
   for (int i = 0; i < strs.length; i++) {
    strs[i] = CsvUtil1.removestr(strs[i]);
   }
  }
 }

 public void writefile() throws Exception {
  for (String[] strs : list) {
   for (int i = 0; i < strs.length; i++) {
    strs[i] = CsvUtil1.removestr(strs[i]);
   }
  }
  Writer writer = new FileWriter(file);
  CSVWriter csvwriter = new CSVWriter(writer);
  csvwriter.writeAll(list);
  csvwriter.close();
  writer.close();
 }

 public void addRecord(String[] strs) throws Exception {
  String[] strArray = { String.valueOf(list.size()), strs[0], strs[1],
    strs[2], strs[3], strs[4] };
  list.add(strArray);
  writefile();
 }

 public void updaterecord(String[] strs) throws Exception {
  String[] srcStrs = list.get(Integer.parseInt(strs[0]));
  for (int i = 1; i < strs.length; i++) {
   srcStrs[i] = strs[i];
  }
  writefile();

 }

}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值