在java中读取.csv文件:缺少几行

时间:2016-10-26 16:47:07

标签: java r csv

我使用R和Java。 使用R,我创建了一个.csv文件。

write.table(allData,file="F:\\Project_file\\testgejavaedbyR1.csv",na="0.000001",sep=",",row.names = FALSE)

这个csv文件看起来像:

"Date","Open","High","Low","Close","Volume","Adj.Close","sma20","ema14"
"2016-10-10",8735.349609,8745.799805,8703.950195,8708.799805,108400,8708.799805,0.000001,0.000001
"2016-10-07",8721.700195,8723.700195,8663.799805,8697.599609,150400,8697.599609,0.000001,0.000001
"2016-10-06",8768.700195,8781.150391,8684.650391,8709.549805,163600,8709.549805,0.000001,0.000001
"2016-10-05",8806.349609,8806.950195,8731.400391,8743.950195,157200,8743.950195,0.000001,0.000001
"2016-10-04",8770,8783.650391,8736.099609,8769.150391,184300,8769.150391,0.000001,0.000001
"2016-10-03",8666.150391,8745.200195,8635,8738.099609,137300,8738.099609,0.000001,0.000001
"2016-09-30",8581.5,8637.150391,8555.200195,8611.150391,181700,8611.150391,0.000001,0.000001
"2016-09-29",8792.700195,8800.650391,8558.25,8591.25,372900,8591.25,0.000001,0.000001
"2016-09-28",8711.200195,8767.049805,8703.150391,8745.150391,165500,8745.150391,0.000001,0.000001
"2016-09-27",8748.900391,8768.5,8690.5,8706.400391,151400,8706.400391,0.000001,0.000001

我尝试使用代码在java上打印此数据:

public void convertToArray(){
        int r=0;
       // items = new String [CountRow()][9];
        items = new String [100][9];


        try{
            BufferedReader reader = new BufferedReader(new FileReader(file));
            String line= null;

           while((line=reader.readLine()) !=null){
            line=reader.readLine();
            StringTokenizer z = new StringTokenizer(line,",");
            while(z.hasMoreTokens()){
                for(int c=0; c<9; c++){
                    items[r][c] = z.nextToken(); 
                }
                r++;
            }
        }   
        }catch(Exception e){
            java.lang.System.out.println(e);
        }
    }

    // print array now

   public void printArray(){
        for(int x=0;x<items.length;x++){
            java.lang.System.out.printf("%s --- ",x);
           // for(int y=0;y<items[x].length;y++){
                 for(int y=0;y<9;y++){
                java.lang.System.out.printf("%s   ",items[x][y]);
            }
            java.lang.System.out.println();
        }
    }  

但是在java中,我的输出是有线连接的,在每行之后,缺少一行, 我的java输出是:

run:
java.lang.ArrayIndexOutOfBoundsException: 100
0 --- "2016-10-10"   8735.349609   8745.799805   8703.950195   8708.799805   108400   8708.799805   0.000001   0.000001   
1 --- "2016-10-06"   8768.700195   8781.150391   8684.650391   8709.549805   163600   8709.549805   0.000001   0.000001   
2 --- "2016-10-04"   8770   8783.650391   8736.099609   8769.150391   184300   8769.150391   0.000001   0.000001   
3 --- "2016-09-30"   8581.5   8637.150391   8555.200195   8611.150391   181700   8611.150391   0.000001   0.000001   
4 --- "2016-09-28"   8711.200195   8767.049805   8703.150391   8745.150391   165500   8745.150391   0.000001   0.000001   
5 --- "2016-09-26"   8807.900391   8809.549805   8715.099609   8723.049805   164000   8723.049805   0.000001   0.000001   
6 --- "2016-09-22"   8873.349609   8893.349609   8837.799805   8867.450195   172800   8867.450195   0.000001   0.000001 

1 个答案:

答案 0 :(得分:2)

你在这里做了两次readLine ......

  while((line=reader.readLine()) !=null){
        line=reader.readLine();

只需删除第二个line=reader.readLine();

即可