我已经编写了从CSV文件中读取内容(用户名和密码)的代码。现在,我需要将这些内容写入另一个CSV文件。请帮我一个好的建议。
public class NewCsvClass {
// private static final String FILE_HEADER = "Username,Password,Result";
// private static final String NEW_LINE_SEPARATOR = "\n";
public static void main( String[] args) throws Exception {
// csv reader aswathy -start
CSVReader reader = new CSVReader(new FileReader("/home/user/Documents/UrmilaDocs/CSV PAck/testCSV.csv"));
String[] nextLine;
WebDriver driver = new FirefoxDriver();
String appUrl = "https://accounts.google.com";
driver.get(appUrl);
driver.manage().window().maximize();
while ((nextLine = reader.readNext()) != null) {
String user_name = nextLine[0];
String pass_word = nextLine[1];
System.out.println("Username: " + user_name);
System.out.println("Password: " + pass_word);
// //stackoverflow
// WebDriver driver = new FirefoxDriver();
// String appUrl = "https://accounts.google.com";
// driver.get(appUrl);
// driver.manage().window().maximize();
WebElement username = driver.findElement(By.xpath(".//*[@id='Email']"));
username.clear();
username.sendKeys(user_name);
driver.findElement(By.xpath(".//*[@id='next']")).click();
Thread.sleep(5000);
// try
try {
WebElement password = driver.findElement(By.xpath(".//*[@id='Passwd']"));
password.clear();
password.sendKeys(pass_word);
driver.findElement(By.xpath(".//*[@id='signIn']")).click();
Thread.sleep(8000);
// click on 'Google Apps' icon
driver.findElement(By.xpath(".//*[@id='gbwa']/div[1]/a")).click();
Thread.sleep(10000);
// Click on 'Gmail' icon to navigate to inbox page
driver.findElement(By.xpath(".//*[@id='gb23']/span[1]")).click();
Thread.sleep(10000);
System.out.println("Login Success");
// Click on user name first letter circle icon
driver.findElement(By.xpath(".//*[@id='gb']/div[1]/div[1]/div[2]/div[4]/div[1]/a/span")).click();
Thread.sleep(5000);
// click on 'Signout' button
driver.findElement(By.xpath(".//*[@id='gb_71']")).click();
Thread.sleep(10000);
System.out.println("Logout Success");
/*
* click on 'Signin with a different account ' option (since, after signing out from // the first user,
* the page is navigated to password entry page, which is supposed to navigate to // username/mailid
* entry page
*/
try {
driver.findElement(By.xpath(".//*[@id='account-chooser-link']")).click();
// driver.findElement(By.xpath(".//*[@id='link-signin-different']/a")).click();
Thread.sleep(10000);
} catch (Exception e) {
}
// /* In 'Choose an Account page', Click on 'Add Account' button */
driver.findElement(By.xpath(".//*[@id='account-chooser-add-account']")).click();
Thread.sleep(10000);
//
} catch (Exception e) {
System.out.println("Login failed!");
driver.findElement(By.xpath(".//*[@id='link-signin-different']/a")).click();
Thread.sleep(10000);
driver.findElement(By.xpath(".//*[@id='account-chooser-add-account']")).click();
Thread.sleep(10000);
} // catch closed
// //closing driver & firefox
// driver.close();
// System.exit(0);
// end
} // while end
// //closing driver & firefox
// driver.close();
// //csv reader aswathy -end
System.exit(0); // closing firefox
// Writing the CSV file to another CSV file
String csv = "/home/user/Documents/UrmilaDocs/CSV PAck/testCSV.csv";
CSVWriter writer = new CSVWriter(new FileWriter(csv));
String[] username = "testuser01.zyx#testuser07#testuser2#testuser01.zyx#tester2.zyx#testernew779".split("#");
String[] password = "testuser0123#user0123#user0123#user0123#user0123#user0123".split("#");
writer.writeNext(username);
writer.writeNext(password);
writer.close();
}
}
答案 0 :(得分:0)
我没有看到import语句,但您使用的是com.csvreader.CsvWriter,即JavaCSV API吗?
如果是,请尝试插入
writer.endRecord();
前
writer.close();