Приведенный ниже код, который я пытаюсь выполнить, повреждает файл excel каждый раз, когда возникает исключение перед закрытием FileInputStream
и FileOutPutStream
. Поэтому я попытался сохранить блок try catch
, а в блоке catch
закрыть FileInputStream
и FileOutPutStream
, но при этом все еще не работает Excel.
Так что я подумал что-то вроде этого, если FileInputStream
не закрыто, то закрыть в блоке catch. Если FileOutPutStream
не закрыто, закройте в блоке catch. Но как этого добиться?
или
Есть ли лучший способ справиться с этой ситуацией?
public void checkingAccountNumber() throws InterruptedException, IOException {
WebDriverWait wait = new WebDriverWait(driver, 5);
FileInputStream fis= new FileInputStream("C:\\Users\\vyerrami\\Desktop\\MA_Conversion_Data.xlsx");
XSSFWorkbook wb= new XSSFWorkbook(fis);
XSSFSheet sheet = wb.getSheet("DEFERRED");
int noOfRows=sheet.getPhysicalNumberOfRows();
try {
for(int i=1; i<noOfRows; i++) {
DataFormatter d = new DataFormatter();
String cellvalue = d.formatCellValue(sheet.getRow(i).getCell(5));
String firstcellvalue=d.formatCellValue(sheet.getRow(i).getCell(0));
if(firstcellvalue.isEmpty()) {
driver.findElement(quickJump).click();
driver.findElement(quickJump).sendKeys("AccountSearch");
Thread.sleep(2000);
driver.findElement(quickJump).sendKeys(Keys.ENTER);
wait.until(ExpectedConditions.invisibilityOf(driver.findElement(loading)));
driver.findElement(accountSearchAccountNumberField).clear();
driver.findElement(accountSearchAccountNumberField).sendKeys(cellvalue);
driver.findElement(accountSearchAccountNumberField).sendKeys(Keys.TAB);
driver.findElement(accountSearchSearchButton).click();
wait.until(ExpectedConditions.invisibilityOf(driver.findElement(loading)));
if (driver.findElements(By.id("AccountSearch:AccountSearchScreen:AccountSearchResultsLV:0:AccountNumber")).size() > 0) {
if (driver.findElement(By.id("AccountSearch:AccountSearchScreen:AccountSearchResultsLV:0:AccountNumber")).getText().contains(cellvalue)) {
driver.findElement(By.id("AccountSearch:AccountSearchScreen:AccountSearchResultsLV:0:AccountNumber"))
.click();
fis.close();
FileOutputStream out = new FileOutputStream(
"C:\\Users\\vyerrami\\Desktop\\MA_Conversion_Data.xlsx");
if(driver.findElements(policyFilePolicyNumber).size()>0) {
sheet.getRow(i).createCell(0)
.setCellValue(driver.findElement(policyFilePolicyNumber).getText());
sheet.getRow(i).createCell(1)
.setCellValue(driver.findElement(policyFileEffectiveDate).getText());
wb.write(out);
out.close();
}
else {
sheet.getRow(i).createCell(0)
.setCellValue("Policy Number is not present");
wb.write(out);
out.close();
}
}
}
else {
FileOutputStream out = new FileOutputStream(
"C:\\Users\\vyerrami\\Desktop\\MA_Conversion_Data.xlsx");
sheet.getRow(i).createCell(0)
.setCellValue("Account Number is not present");
wb.write(out);
out.close();
}
}
}
}
catch(Exception e)
{
//i want to write the code if fis is not closed then i have to close
//i want to write the code is out is not closed then i have to close
}
}
РЕДАКТИРОВАТЬ КОД, КОТОРЫЙ РАБОТАЕТ, НИЖЕ:
FileInputStream fis=null;
FileOutputStream out1=null;
XSSFWorkbook workbook= null;
XSSFSheet sheet = null;
try {
fis = new FileInputStream("C:\\Users\\vyerrami\\Desktop\\VINS.xlsx");
out1 = new FileOutputStream("C:\\Users\\vyerrami\\Desktop\\VINS.xlsx");
workbook = new XSSFWorkbook(fis);
sheet = workbook.getSheet("VINS");
int noOfRows = sheet.getPhysicalNumberOfRows();
for (int i = 0; i < 100; i++) {
driver.findElement(createVehicleButton).click();
wait.until(ExpectedConditions.invisibilityOf(driver.findElement(loading)));
driver.findElement(EnterVinNumber).clear();
driver.findElement(EnterVinNumber).sendKeys(sheet.getRow(i).getCell(0).getStringCellValue());
driver.findElement(EnterVinNumber).sendKeys(Keys.TAB);
wait.until(ExpectedConditions.invisibilityOf(driver.findElement(loading)));
driver.findElement(vehicleInquiryButton).click();
wait.until(ExpectedConditions.invisibilityOf(driver.findElement(loading)));
System.out.println(driver
.findElement(By.xpath(
"//*[@id='VehicleInquiryReportPopup:2-body']/div/div/table[2]/tbody/tr/td[2]/div"))
.getText());
System.out.println(driver
.findElement(By.xpath(
"//*[@id='VehicleInquiryReportPopup:2-body']/div/div/table[2]/tbody/tr/td[3]/div"))
.getText());
if (driver
.findElement(By.xpath(
"//*[@id='VehicleInquiryReportPopup:2-body']/div/div/table[2]/tbody/tr/td[2]/div"))
.getText().equalsIgnoreCase("null null")
|| !driver.findElement(By.xpath(
"//*[@id='VehicleInquiryReportPopup:2-body']/div/div/table[2]/tbody/tr/td[3]/div"))
.getText().equalsIgnoreCase("null null") && (sheet.getRow(i).getCell(1).getStringCellValue().isEmpty())) {
fis.close();
sheet.getRow(i).createCell(1).setCellValue("Not Leased");
workbook.write(out1);
out1.close();
workbook.close();
}
else {
fis.close();
sheet.getRow(i).createCell(1).setCellValue("Leased");
workbook.write(out1);
out1.close();
workbook.close();
}
driver.findElement(By.id("VehicleInquiryReportPopup:__crumb__")).click();
wait.until(ExpectedConditions.invisibilityOf(driver.findElement(loading)));
driver.findElement(By.xpath(
"//*[@id='SubmissionWizard:LOBWizardStepGroup:LineWizardStepSet:PAVehiclesScreen:PAVehiclesPanelSet:VehiclesListDetailPanel:VehiclesLV-body']/div/div/table/tbody/tr/td/div/img"))
.click();
driver.findElement(By.id(
"SubmissionWizard:LOBWizardStepGroup:LineWizardStepSet:PAVehiclesScreen:PAVehiclesPanelSet:VehiclesListDetailPanel_tb:Remove"))
.click();
wait.until(ExpectedConditions.invisibilityOf(driver.findElement(loading)));
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if(fis!=null) {
fis.close();
}
if(out1!=null) {
out1.close();
}
}
}