csvfile

package changeString;import java.io.BufferedReader;import java.io.FileReader;import java.util.ArrayList;import java.util.Iterator;import java.util.List;public class CsvUtil { private String fileName = null; private BufferedReader br = null; private Listlist = new ArrayList();

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();

}

}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 14,357评论 0 33
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,159评论 19 139
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 11,891评论 0 17
  • 一、 1、请用Java写一个冒泡排序方法 【参考答案】 public static void Bubble(int...
    独云阅读 5,264评论 0 6
  • 淮镇阅读 1,535评论 0 2

友情链接更多精彩内容