您的位置:首页 > 编程语言 > Java开发

java:读写csv文件

2016-01-08 04:40 375 查看
学期projet总结:

做这个projet的第一步就是把数据读进来。

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

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


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

[code]// four arraylists for saving the values of source,target,distance and dang
public 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:

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


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

[code]    // 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文件。首先要在程序中引入这个包,主要代码如下:

[code]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类型

[code]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();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: