csvjava的简单介绍

发布时间:2023-01-09

本文目录一览:

  1. JAVA中如何使用创建CSV文件
  2. 如何用java解析CSV文件
  3. java读取csv文件
  4. java对操作csv文件

JAVA中如何使用创建CSV文件

csv文件又叫逗号文件 你只要new一个File文件,文件路径给好,文件名随便起,文件名后缀是csv就可以了 然后把这个文件放到流对象中,你要写什么直接对这个流进行操作即可

如何用java解析CSV文件

思想:先获取csv文件的路径,通过BufferedReader类去读该路径中的文件,使用readLine方法进行逐行读取。 注意:使用readLine方法后会自动转到下一行。因此在判断是否为空后得先将读取到的内容赋值给一变量,在循环中使用该变量即可。

public static void main(String[] args) {
    File csv = new File("C:\\Users\\chenxumin\\Desktop\\Result.csv"); // CSV文件路径
    BufferedReader br = null;
    try {
        br = new BufferedReader(new FileReader(csv));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    String line = "";
    String everyLine = "";
    try {
        List<String> allString = new ArrayList();
        while ((line = br.readLine()) != null) { //读取到的内容给line变量
            everyLine = line;
            System.out.println(everyLine);
            allString.add(everyLine);
        }
        System.out.println("csv表格中所有行数:" + allString.size());
    } catch (IOException e) {
        e.printStackTrace();
    }
}

java读取csv文件

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.*;
public class Test {
    public static void main(String[] args) {
        Hashtable<String, String[]> dict = new Hashtable<String, String[]>();
        try {
            BufferedReader reader = new BufferedReader(new FileReader("test.csv"));
            String line = null;
            while ((line = reader.readLine()) != null) {
                String item[] = line.split(",");
                String item2[] = new String[19];
                System.arraycopy(item, 1, item2, 0, 19);
                dict.put(item[0], item2);
            }
            Enumeration e2 = dict.keys();
            while (e2.hasMoreElements()) {
                String key = (String) e2.nextElement();
                System.out.println(key);
                String[] dd = (String[]) dict.get(key);
                for (int i = 0; i < dd.length; i++) {
                    System.out.print(dd[i] + "\t");
                }
                System.out.println();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

java对操作csv文件

import java.io.BufferedReader;
import java.io.FileReader;
public class Test {
    public void test(int row, int col) {
        try {
            BufferedReader reader = new BufferedReader(new FileReader("C:\\a.csv")); //换成你的文件名
            //reader.readLine(); //第一行信息,为标题信息,不用,如果需要,注释掉
            String line = null;
            int index = 0;
            while ((line = reader.readLine()) != null) {
                String item[] = line.split(" "); //CSV格式文件为逗号分隔符文件,这里根据逗号切分
                if (index == row - 1) {
                    if (item.length >= col - 1) {
                        String last = item[col - 1]; //这就是你要的数据了
                        System.out.println(last);
                    }
                    //int value = Integer.parseInt(last); //如果是数值,可以转化为数值
                }
                index++;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
        Test test = new Test();
        test.test(3, 2);
    }
}

你的数据格式有问题,空格的个数不确定,没法每行用空格分隔。以下是我调整后的数据格式每行的数据以一个空格分隔,test方法传入的参数一次是,行,列: 1 电机 1 2 WBS 2 3 PID 3 4 CP 5 社供出 6 原価実绩 7 社供WC 8 外注费 9 直材费 10 自家制品 11 直経费 12 その他 13 注残 14 注残