Mr.*_*hoi 3 java fileoutputstream apache-poi
我正在使用 java SE eclipse。据我所知,当没有由参数命名的文件时 FileOutputStream 构造函数创建由参数命名的新文件。但是,随着继续,我看到 FileOutputStream 产生异常 FileNotFoundException。我真的不知道为什么需要这个例外。我的知识有问题吗?
我的代码如下(制作工作簿并写入文件。在此代码中,虽然没有文件“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()
Run Code Online (Sandbox Code Playgroud)
如果有什么奇怪的,请告诉我,谢谢
如果文件不存在且无法创建(doc),它将抛出 FileNotFoundException ,但如果可以,它将创建它。为了确保您可能应该在创建 FileOutputStream 之前首先测试该文件是否存在(如果不存在,则使用 createNewFile() 创建)
File yourFile = new File("score.txt");
yourFile.createNewFile();
FileOutputStream oFile = new FileOutputStream(yourFile, false);
Run Code Online (Sandbox Code Playgroud)
从这里回答:Java FileOutputStream 如果不存在则创建文件
| 归档时间: |
|
| 查看次数: |
11154 次 |
| 最近记录: |