2016-09-17 70 views
0

我有两个csv文件。
CSV File1 = csvFrom,//它有两列,column1 = Email(只有5封电子邮件),Column2 =密码
CSV File2 = csvSendTo //它只有一列= Email。 (数千封电子邮件)。在java中读取并解析CSV文件

我想读取csvFrom文件,我想要第一个电子邮件ID和它的密码从第一个文件。然后从第二个csvSendTo文件发送20封电子邮件。想要将第一封电子邮件ID和密码的电子邮件发送到这20封电子邮件。我可以用一个电子邮件ID手动发送电子邮件。但是当我试图从csv文件读取它时,它给了我一个NullPointerException。下面是我的代码:我只是粘贴了这里给我一个错误的部分。有谁可以在这里指导我吗?这个for循环在这里是错误的,但我有点困惑,所以无法用精确的循环代替。

BufferedReader br1=null; 
    BufferedReader br2=null; 
    String line1="",line2=""; 
    String csvSplitBy=","; 
    String strMailFrom="",strPassword=""; 
    int countCSVFrom=0,countCSVSendTo=0; 
    System.out.println("strCSVFrom=" + strCSVFrom + ", strcsvSendTo=" + strCSVSendTo); 
    try{ 
     br1=new BufferedReader(new FileReader(strCSVFrom)); 
     br2=new BufferedReader(new FileReader(strCSVSendTo)); 
     String[] strarrFromEmail; 
     while((line1=br1.readLine())!=null){ 
      countCSVFrom+=1; 
      strarrFromEmail=line1.split(csvSplitBy); 
     // for(int i=countCSVFrom-1;i<=countCSVFrom;i++){ 
     //  strMailFrom=strarrFromEmail[i]; 
     //  strPassword=strarrFromEmail[i+1]; //While here its ArrayIndexOutOfBounds Exception 
     // } 
     //what is the correct thing to write it over here instead of for loop? 
     } 
     System.out.println("countcsvfrom="+countCSVFrom + ", line1=" + line1.toString()); //Giving me an error of NullPointerException over here. 

     System.out.println("strFrom="+strMailFrom + ", strPassword="+strPassword); 
     while((line2=br2.readLine())!=null){ 
      countCSVSendTo+=1; 
     } 
     System.out.println("countcsvsendto="+countCSVSendTo); 
    }catch(FileNotFoundException fnfe){ 
     fnfe.printStackTrace(); 
    }catch(IOException ioe){ 
     ioe.printStackTrace(); 
    } 
+0

堆栈跟踪或行号错误发生在这里是必需的 – Sanka

+0

我已更新我的问题。 –

回答

0

你可以写:

//your code here and try catch block below 
try { 
    List<String[]> csvLines = new ArrayList<String[]>(); 
    Files.lines(Paths.get(strCSVFrom)).map(e -> e.split(csvSplitBy)).forEach(csvLines.add(e)); 
} catch (Exception) { 
    //exception handling 
} 
1

第二System.out.println,只是第while环的右括号后,给人的NPE。它使用line1,所述while环路保证的终止条件将在该点处为null

+0

谢谢你的帮助。是的,我得到了,但没有得到如何解决它。 –