使用OpenCSV根据特定行值

时间:2015-11-27 06:40:17

标签: java csv opencsv

我有一个CSV文件,我使用OpenCSV进行解析。 CSV文件如下所示:

  

δ,θ,signal_1,signal_2,signal_3,signal_4,signal_5,signal_6,值1,值,死亡   71239,56366,8654,21170,16053,27229,5835,8834,3.511244,3.511244,0   234145,160964,16164,20320,29410,17088,9041,8885,5.473104,4.165198,0   76576,23670,11216,22239,8831,15797,13216,7872,2.680331,2.380573,0   44504,39475,12008,18694,14639,25165,11716,24879,2.696564,2.685185,0   275603,31948,10360,7867,15249,16791,8526,4918,2.095088,2.490662,0   80893,16847,7368,19869,5425,11039,6720,5553,3.105438,2.632364,0   70494,323693,61661,3887,17667,35640,16069,8686,18.3219,9.953226,1   808498,57853,13771,15957,13831,20629,15981,11177,4.18285,10.971,1   900628,263343,38948,48837,11122,12378,29276,25673,23.67767,15.39414,1   306369,11836,12448,16708,14225,38644,56110,24842,0.8320562,9.564192,1   2337196,354879,28332,94271,31804,40325,27890,35445,11.15831,11.88935,1   129047,45750,2640,13725,6823,17161,8363,8970,6.705262,6.231878,1   275603,31948,10360,7867,15249,16791,8526,4918,2.095088,2.490662,2   80893,16847,7368,19869,5425,11039,6720,5553,3.105438,2.632364,2   80494,312693,61261,3887,17667,35640,16069,8686,18.3219,9.953226,2   990628,123343,12948,48837,11122,12378,29276,25673,23.67767,15.39414,2   886369,56836,12348,16708,14225,38644,56110,24842,0.8320562,9.564192,2   4437196,234879,34532,94271,31804,40325,27890,35445,11.15831,11.88935,2

这里,最后一列是死亡值,并且在某些行(完全是任意的)之后继续增加。我想找到第一次死亡发生在哪一行并取出前三行并将其写入单独的csv文件中。所以在这里,第一次死亡发生在

  

70494,323693,61661,3887,17667,35640,16069,8686,18.3219,9.953226,1

所以我会考虑前三行:

  

44504,39475,12008,18694,14639,25165,11716,24879,2.696564,2.685185,0   275603,31948,10360,7867,15249,16791,8526,4918,2.095088,2.490662,0   80893,16847,7368,19869,5425,11039,6720,5553,3.105438,2.632364,0

并将它们写入单独的CSV文件(假设这是newCSV.csv)。并且将对death = 2和death = 3执行相同操作并继续将它们附加到该newCSV.csv文件中。 我可以使用OpenCSV逐行读取它们,但不知道如何根据死亡值检测行。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

这是您的完整解决方案,

您的主要逻辑

FileInputStream fin = new FileInputStream("C/inputFile.csv");
        FileOutputStream fout = new FileOutputStream("C/newCSV.csv");

        BufferedReader br = new BufferedReader(new InputStreamReader(fin));

        MyObject[] last3MyObject = new MyObject[3];
        int index = 0;
        String str = br.readLine();//read Filed Header Value...
        String fieldHeader = str;
        fout.write(fieldHeader.getBytes());//write header into output file...

        str = br.readLine();

        int currentDeathValue = 0;
        int previousDeathValue = 0;

        while(str != null){

            MyObject myObject = new MyObject();

            String [] tkn = str.split(",");

            myObject.setDelta(Long.parseLong(tkn[0]));
            myObject.setTheta(Long.parseLong(tkn[1]));
            myObject.setSignal_1(Long.parseLong(tkn[2]));
            myObject.setSignal_2(Long.parseLong(tkn[3]));
            myObject.setSignal_3(Long.parseLong(tkn[4]));
            myObject.setSignal_4(Long.parseLong(tkn[5]));
            myObject.setSignal_5(Long.parseLong(tkn[6]));
            myObject.setSignal_6(Long.parseLong(tkn[7]));
            myObject.setValue_1(Double.parseDouble(tkn[8]));
            myObject.setValue_2(Double.parseDouble(tkn[9]));

            currentDeathValue = Integer.parseInt(tkn[10]);

            if(currentDeathValue != previousDeathValue){
                for(MyObject mylast3Object  : last3MyObject){

                    fout.write(mylast3Object.toString().getBytes());
                    fout.write("\n".getBytes());
                }
            }

            previousDeathValue = currentDeathValue;

            myObject.setDeath(currentDeathValue);

            if(index == 3){
                index = 0;
            }

            last3MyObject[index++] = myObject; 

            str = br.readLine();

制作一个Bean类,

class MyObject{

    private long delta;
    private long theta;
    private long signal_1;
    private long signal_2;
    private long signal_3;
    private long signal_4;
    private long signal_5;
    private long signal_6;
    private double value_1;
    private double value_2;
    private int death;
    public long getDelta() {
        return delta;
    }
    public void setDelta(long delta) {
        this.delta = delta;
    }
    public long getTheta() {
        return theta;
    }
    public void setTheta(long theta) {
        this.theta = theta;
    }
    public long getSignal_1() {
        return signal_1;
    }
    public void setSignal_1(long signal_1) {
        this.signal_1 = signal_1;
    }
    public long getSignal_2() {
        return signal_2;
    }
    public void setSignal_2(long signal_2) {
        this.signal_2 = signal_2;
    }
    public long getSignal_3() {
        return signal_3;
    }
    public void setSignal_3(long signal_3) {
        this.signal_3 = signal_3;
    }
    public long getSignal_4() {
        return signal_4;
    }
    public void setSignal_4(long signal_4) {
        this.signal_4 = signal_4;
    }
    public long getSignal_5() {
        return signal_5;
    }
    public void setSignal_5(long signal_5) {
        this.signal_5 = signal_5;
    }
    public long getSignal_6() {
        return signal_6;
    }
    public void setSignal_6(long signal_6) {
        this.signal_6 = signal_6;
    }
    public double getValue_1() {
        return value_1;
    }
    public void setValue_1(double value_1) {
        this.value_1 = value_1;
    }
    public double getValue_2() {
        return value_2;
    }
    public void setValue_2(double value_2) {
        this.value_2 = value_2;
    }
    public int getDeath() {
        return death;
    }
    public void setDeath(int death) {
        this.death = death;
    }

    @Override
    public String toString() {
        return this.delta+","+this.theta+","+this.signal_1+","+this.signal_2+","+this.signal_3+","+this.signal_4+","+this.signal_5+","+this.signal_6+","+this.value_1+","+this.value_2+","+this.death;
    }

}

<强> EDITED ....

while(str != null){
            //System.out.println("IN");
            MyObject myObject = new MyObject();

            String [] tkn = str.split(",");

            myObject.setDelta(Long.parseLong(tkn[0]));
            myObject.setTheta(Long.parseLong(tkn[1]));
            myObject.setSignal_1(Long.parseLong(tkn[2]));
            myObject.setSignal_2(Long.parseLong(tkn[3]));
            myObject.setSignal_3(Long.parseLong(tkn[4]));
            myObject.setSignal_4(Long.parseLong(tkn[5]));
            myObject.setSignal_5(Long.parseLong(tkn[6]));
            myObject.setSignal_6(Long.parseLong(tkn[7]));
            myObject.setValue_1(Double.parseDouble(tkn[8]));
            myObject.setValue_2(Double.parseDouble(tkn[9]));
            currentDeathValue = Integer.parseInt(tkn[10]);
            myObject.setDeath(currentDeathValue);

            if(flag && (previousDeathValue == currentDeathValue)){
                if(index < 3){
                    last3MyObject[index++] = myObject;
                }else{
                    flag = false;
                    for(MyObject mylast3Object  : last3MyObject){
                        System.out.println(mylast3Object);
                        fout.write(mylast3Object.toString().getBytes());
                        fout.write("\n".getBytes());
                    }

                }
            }

            if(currentDeathValue != previousDeathValue){
                flag = true;
                index = 0;
            }

            previousDeathValue = currentDeathValue;

            str = br.readLine();
        }