gpt4 book ai didi

java - 用java读取并解析CSV文件

转载 作者:行者123 更新时间:2023-12-01 09:29:43 25 4
gpt4 key购买 nike

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

我正在尝试读取 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();
}

最佳答案

第二个 System.out.println 位于第一个 while 循环的右大括号之后,给出 NPE。它使用 line1,该 while 循环的终止条件保证此时将为 null

关于java - 用java读取并解析CSV文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39546364/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com