Is there a faster way to use csv reader in Java?

Viewed 79

I need to open a csv file in more parts, each one by 5,000 samples and then plot them. To go back and forward on the signal each time I click a button I have to instantiate a new reader and than I skip to the point I need. My signal is big, is about 135,000 samples so csvReader.skip() method is very slow when I work with last samples. But to go back I can't delete lines, so each time my iterator needs to be re-instantiated. I noticed that skip uses a for loop? Is there a better way to overtake this problem? Here is my code:

    public void updateSign(int segmento) {
    Log.d("segmento", Integer.toString(segmento));
    //check if I am in the signal length
    if (segmento>0 && (float)(segmento-1)<=(float)TOTAL/normaLen)
    {
        try {
            reader = new CSVReader(new FileReader(new File(patty)));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        List<Integer> sign = new ArrayList<>();
        //this is the point of the signal where i finish
        int len = segmento * normaLen;
        //check if i am at the end of the signal
        if (len >= TOTAL) {
            len = TOTAL;
            segmento=0;
            avanti.setValue(false);
            System.out.println(avanti.getValue());
        } else {
            lines = TOTAL - len;

            avanti.setValue(true);
            System.out.println(avanti.getValue());
        }
        //the int to i need to skip
        int skipper = (segmento-1)*normaLen;
        try {
            System.out.println("pre skip");
            reader.skip(skipper);
            System.out.println("post skip");
        } catch (IOException e) {
            e.printStackTrace();
        }
        //my iterator
        it = reader.iterator();
        System.out.println("iteratore fatto");
        //loop to build my mini-signal to plot
        //having only 5,000 sample it is fast enaugh
        for (int i = skipper; i < len-1; i++) {

            if (i>=(segmento-1)*normaLen) {

                sign.add(Integer.parseInt(it.next()[0]));

            }
            else
            {

                it.next();
                System.out.println("non ha funzionato lo skip");
            }
        }
        System.out.println("ciclo for: too much fatica?");
        //set sign to be plotted by my fragment
        liveSign.setValue(sign);
    }
}

Thanks in advance!

0 Answers
Related