FileoutputStream FileNotFoundException
Asked Answered
T

2

6

I'm using java SE eclipse. As I know, When there are no file named by parameter FileOutputStream constructor create new file named by parameter. However, with proceeding I see that FileOutputStream make exception FileNotFoundException. I really don't know Why this exception needed. Anything wrong with my knowledge?

My code is following(make WorkBook and write into file. In this code, although there are no file "data.xlsx", FileOutpuStream make file "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()

If anything weird, please let me know, thank you

Teacher answered 31/12, 2015 at 8:12 Comment(6)
Share the full Exception please? Maybe you're inside a read-only directory?Dichogamy
What is wb exactly ? Please post full relevant code .Caroche
No, it's a normal directory. Do you need more code? Do you have any problem about my code?Teacher
Does 'new File("data.xlsx").createNewFile();' work ?Caroche
code is fine - but the exception will give further details as to why it failedDichogamy
yes. 'new File("data.xlsx").createNewFile();' make new file.Teacher
A
10

It will throw a FileNotFoundException if the file doesn't exist and cannot be created (doc), but it will create it if it can. To be sure you probably should first test that the file exists before you create the FileOutputStream (and create with createNewFile() if it doesn't)

File yourFile = new File("score.txt");
yourFile.createNewFile();
FileOutputStream oFile = new FileOutputStream(yourFile, false); 

Answer from here: Java FileOutputStream Create File if not exists

Augusto answered 31/12, 2015 at 8:15 Comment(2)
Does it mean case by case depends on the parameter FileOutputStream make new file or not??Teacher
It means that it should normally create the file if it doesn't exists but if it is the case that it can't create the file (ex.: no permissions) will throw that exceptionAugusto
M
1

There is another case, where new FileOutputStream("...") throws a FileNotFoundException, i.e. on Windows, when the file is existing, but file attribute hidden is set.

Here, there is no way out, but resetting the hidden attribute before opening the file stream, like

Files.setAttribute(yourFile.toPath(), "dos:hidden", false); 
Misdo answered 7/9, 2021 at 12:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.