我正在使用Java SE Eclipse。
据我所知,当没有以参数命名的文件时,FileOutputStream构造函数将创建一个以参数命名的新文件。但是,在进行过程中,我发现FileOutputStream会抛出FileNotFoundException异常。我真的不知道为什么需要这个异常。我的知识有什么问题吗?
以下是我的代码(创建工作簿并将其写入文件)。在此代码中,尽管没有文件“data.xlsx”,但FileOutpuStream还是创建了文件“data.xlsx”。
如果有任何奇怪的情况,请告诉我,谢谢。
以下是我的代码(创建工作簿并将其写入文件)。在此代码中,尽管没有文件“data.xlsx”,但FileOutpuStream还是创建了文件“data.xlsx”。
public ExcelData() {
try {
fileIn = new FileInputStream("data.xlsx");
try {
wb = WorkbookFactory.create(fileIn);
sheet1 = wb.getSheet(Constant.SHEET1_NAME);
sheet2 = wb.getSheet(Constant.SHEET2_NAME);
} catch (EncryptedDocumentException | InvalidFormatException | IOException e) {
e.printStackTrace();
} // if there is file, copy data into workbook
} catch (FileNotFoundException e1) {
initWb();
try {
fileOut = new FileOutputStream("data.xlsx");
wb.write(fileOut);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} // if there is not file, create init workbook
} // ExcelData()
如果有任何奇怪的情况,请告诉我,谢谢。