我需要将控制台输出导出到 Java 中的 2 个不同文件.在第一个文件中,我能够获取数据,而第二个文件为空 [英] I need to export console output to 2 different files in Java. In first file I'm able to get data and the second file is empty
问题描述
我需要 10 个人的详细信息,每个人的内部有不同数量的详细信息.我需要所有这些,但数据是 13 行.所以我需要所有这 13 行并像段落一样打印数据.我需要将这些数据导出到文件中,以便我可以将它们用于不同的目的.
I need details of 10 persons and inside every person there are different number of details. I need all that that but the data is in 13 rows. so i need to all these 13 rows and print data like a paragraph. I need to export this data to files so i can use them for different purposes.
driver.findElement(By.id("searchActivitySubmitButton")).click();
Map<String, String> map = new LinkedHashMap<>();
PrintStream prevConsole = System.out;
PrintStream myconsole = new PrintStream(new File("C:\FileName15.txt")); // exporting data in first file
System.setOut(myconsole);
for (int i = 1; i <= 10; i++) {
driver.findElement(By.xpath("(//a[contains(@id,'requestAchFileUploadDetails')])[" + i + "]")).click(); // checks the data of the first 10 people whom data i need
driver.findElement(By.xpath("//a[text()='See transactions']")).click();
Thread.sleep(3700);
List<WebElement> listRows = driver.findElements(
By.xpath("//table[@id='transactionActivityTable']//tr[contains(@id,'transaction_')]")); // for every row there are multiple columns which all data need to extracted.
for (int j = 1; j <= listRows.size(); j++) {
driver.findElement(By.xpath(
"(//tr[contains(@id,'transaction_')]//i[contains(@id,'icon-activitySeeDetails')])[" + j + "]"))
.click();
for (int k = 1; k <= 13; k++) {
String keys = driver.findElement(By.xpath("(//dt[contains(@id,'transactionDetailDataSet')]/span/span)[" + k + "]")).getText(); // inside every person there are 13 rows.
String value = driver
.findElement(By.xpath("//dl[@class='dataset row stack center']//dd[" + k + "]")).getText();
map.put(keys + " ; ", value);
Thread.sleep(1000);
}
for (Map.Entry<String, String> m : map.entrySet()) {
System.out.println(m.getKey() + " " + m.getValue());
}
System.out.println("
");
}
Thread.sleep(1000);
driver.navigate().back();
Thread.sleep(3000);
driver.navigate().back();
Thread.sleep(5000);
}
System.setOut(prevConsole);
myconsole.close();
PrintStream mycons = new PrintStream(new File("C:\chase\sftp\ach_transactions_summary\Duplicate.txt")); // trying exporting data in second file
System.setOut(mycons);
//数据仅在第一个文件中导出,第二个文件为空.
// Data is exporting only in first file and second file is empty.
推荐答案
Nothing output to the second file 因为您在定义 System.setOut
后没有向 System.out
发送任何内容通过 System.setOut(mycons);
Nothing outputted to the second file since you send nothing to System.out
after defining System.setOut
to the second file by System.setOut(mycons);
这篇关于我需要将控制台输出导出到 Java 中的 2 个不同文件.在第一个文件中,我能够获取数据,而第二个文件为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!