java CSV 文件的操作_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > java CSV 文件的操作

java CSV 文件的操作

 2013/7/29 19:09:28  dugu61888  程序员俱乐部  我要评论(0)
  • 摘要:packagetest;importjava.io.BufferedReader;importjava.io.File;importjava.io.FileReader;importjava.util.ArrayList;importjava.util.Iterator;importjava.util.List;publicclasscsvdemotest{privateStringfileName=null;privateBufferedReaderbr=null
  • 标签:文件 Java 操作
class="java" name="code">package test;

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

public class csvdemotest {

	private String fileName = null;
	private BufferedReader br = null;
	private List<String> list = new ArrayList<String>();

	public File getFile(String name) {

		File file = new File(name);
		System.out.println(file);
		return file;

	}

	public csvdemotest(String fileName) throws Exception {
		this.fileName = fileName;
		br = new BufferedReader(new FileReader(getFile(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 static void main(String[] args) throws Exception {
		csvdemotest util = new csvdemotest(
				"D:\\DE39046_PEN_TACTICAL_RETIRE_FIX.csv");
		util.getFile("D:\\");

		int rowNum = util.getRowNum();
		int colNum = util.getColNum();
		String x = util.getRow(0);
		String y = util.getCol(0);
		String []temp=y.split(",");
		char []a=y.toCharArray();
		for(String s:temp){
			System.out.println(s);
		}
		for(char s:a){
			System.out.println(s);
		}
		System.out.println(temp.length);
	}
/*		System.out.println("rowNum:" + rowNum);
		System.out.println("colNum:" + colNum);
		System.out.println("x:" + x);
		System.out.println("y:" + y);

		for (int i = 1; i < rowNum; i++) {
			for (int j = 0; j < colNum; j++) {
				System.out.println("result[" + i + "|" + j + "]:"
						+ util.getString(i, j));
			}
		}*/

	}

?

发表评论
用户名: 匿名