gpt4 book ai didi

java - 如果以下代码中发生错误,如何关闭 Excel 文件而不崩溃

转载 作者:行者123 更新时间:2023-12-01 19:31:38 25 4
gpt4 key购买 nike

只要在关闭 FileInputStreamFileOutPutStream 之前发生异常,我尝试执行的以下代码就会损坏 Excel 文件。所以我尝试保留 try catch block ,并在 catch block 中关闭 FileInputStreamFileOutPutStream,但 excel 仍然崩溃。

所以我想如果 FileInputStream 未关闭则在 catch block 中关闭。如果 FileOutPutStream 未关闭,则在 catch block 中关闭。但如何实现这一目标呢?

有没有更好的方法来处理这种情况?

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();
}
}

}

最佳答案

我想,您正在寻找 try with resource 。它是在 try block 中打开流并在 finally block 中关闭流的糖语法。这样,无论 try block 中发生什么,流都会关闭。

在大多数情况下,您应该尽可能晚地打开流并尽快关闭它们。尝试创建更小的方法,以便以后更容易调试它们。

关于java - 如果以下代码中发生错误,如何关闭 Excel 文件而不崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59651451/

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