java:读写csv文件

来源:互联网 发布:中英文识别 python 编辑:程序博客网 时间:2024/04/27 16:07

学期projet总结:
做这个projet的第一步就是把数据读进来。
为了保存与点相关的数据,我定义两个ArrayList:

// two arraylists for saving the values of longitude and latitudepublic static ArrayList<Double> longitude = new ArrayList<Double>();public static ArrayList<Double> latitude = new ArrayList<Double>();

为了保存与线相关的数据,我定义四个ArrayList:

// four arraylists for saving the values of source,target,distance and dangpublic static ArrayList<Integer> source = new ArrayList<Integer>();public static ArrayList<Integer> target = new ArrayList<Integer>();public static ArrayList<Integer> distance = new ArrayList<Integer>();public static ArrayList<Integer> danger = new ArrayList<Integer>();

其实这里更好的方法是定义一个node类,node类有三个属性:index,longitude,latitude;定义一个arc类,arc类有四个属性:source,target,distance,danger。然后定义两个ArrayList:

public static ArrayList<Node> distance = new ArrayList<Node>();public static ArrayList<Arc> danger = new ArrayList<Arc>();

读csv文件:csv文件的每行数据都有分隔符,一般是逗号,也有其他情况,这里老师给的数据的分隔符是“\t”。代码如下:

    // read node file    public static void readNode() {        try {            BufferedReader reader = new BufferedReader(new FileReader(                    "文件路径"));            //csv文件的第一行如果是数据的说明信息的话,需要把它剔除                  //reader.readLine();// 如果没有说明信息,就把这行代码注释掉            String line = null;            while ((line = reader.readLine()) != null) {                // read data to sting array把每行数据读入到一个string数组里                String item[] = line.split("\t");//数据间的分隔符是"\t"                // change the value type from sting to double                double dLongitude = Double.parseDouble(item[item.length - 2]);                double dLatitude = Double.parseDouble(item[item.length - 1]);                // add value to arraylist                longitude.add(dLongitude);                latitude.add(dLatitude);            }            reader.close();        } catch (Exception e) {            e.printStackTrace();        }    }

在java中有一个专门操作csv文件的包,叫javacsv.jar,接下来介绍一下用这个包怎么读csv文件。首先要在程序中引入这个包,主要代码如下:

ArrayList<Double> longitude = new ArrayList<Double>();ArrayList<Double> latitude = new ArrayList<Double>();try {    CsvReader reader = new CsvReader("berlin_noeuds.csv", '\t',            Charset.forName("UTF-8"));//文件路径,分隔符,编码格式    // reader.readHeaders(); //跳过表头 如果需要表头的话,不要写这句。    while (reader.readRecord()) { // 逐行读入数据        String item[] = reader.getValues();//保存每行数据        double dLongitude = Double.parseDouble(item[item.length - 2]);        double dLatitude = Double.parseDouble(item[item.length - 1]);        longitude.add(dLongitude);        latitude.add(dLatitude);    }    reader.close();} catch (Exception e) {    e.printStackTrace();}       

用这个jar包保存csv文件也很方便。在projet中如果选中一条线,可以更改线的距离和危险系数,然后要把新的arc数据保存成csv文件,几行代码就可以搞定。要注意的是,一行一行的写数据,并且数据格式是String类型

CsvWriter wrArc = new CsvWriter("文件路径",'\t', Charset.forName("UTF-8"));for (int i = 0; i < ReadFile.source.size(); i++) {    String[] contents = { ReadFile.source.get(i).toString(),            ReadFile.target.get(i).toString(),            ReadFile.distance.get(i).toString(),            ReadFile.danger.get(i).toString() };    try {        wrArc.writeRecord(contents);    } catch (Exception e) {        e.printStackTrace();    }}wrArc.close();
0 0
原创粉丝点击