第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

FileOutputStream 拋出 FileNotFoundException

FileOutputStream 拋出 FileNotFoundException

慕碼人2483693 2021-12-10 10:55:06
我正在嘗試與目錄一起創(chuàng)建一個(gè)新文件,但是當(dāng)我調(diào)用“fos = new FileOutputStream(file);”時(shí) 它總是拋出找不到文件的錯(cuò)誤。這是代碼FileOutputStream fos = null;String getName = "User";String filePath="D:/New file";File file;Date date = new Date();ByteArrayOutputStream outputStream = new ByteArrayOutputStream();String headerDate = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(date);try {  WritableWorkbook w = Workbook.createWorkbook(outputStream);  WritableSheet s = w.createSheet("Report generate", 0);  s.addCell(new Label(0, 0, "New File" + getName));  s.addCell(new Label(0, 2, "Response Date: " + headerDate));  w.write();  w.close();  String resultFileName = "NewFileToGenerate" +getName+headerDate+ ".xls";  String fileName = filePath.concat(resultFileName);  file = new File(fileName);  file.mkdirs();  file.createNewFile();  fos = new FileOutputStream(file);  ByteArrayOutputStream baos = new ByteArrayOutputStream();  baos = outputStream;  // Put data in your baos  baos.writeTo(fos);} catch (Exception e) {} finally {  outputStream.close();  fos.close();}在這里,我有文件路徑,但在該文件路徑中,我必須通過在其中附加日期來(lái)創(chuàng)建另一個(gè)文件夾,然后我必須保存文件。這是堆棧跟蹤D:/New file/NewFileToGenerateUser26/2018 20:00:14.xls(是目錄)
查看完整描述

3 回答

?
藍(lán)山帝景

TA貢獻(xiàn)1843條經(jīng)驗(yàn) 獲得超7個(gè)贊

當(dāng)你使用


file.makeDirs();

它創(chuàng)建了所有不存在的目錄,包括"NewFileToGenerate" +getName+headerDate+ ".xls". 是的,您要?jiǎng)?chuàng)建的文件是作為目錄創(chuàng)建的。


然后你調(diào)用了 file.createNewFile(),它會(huì)返回 false,因?yàn)榇嬖谂c文件同名的目錄。


嘗試對(duì)目錄使用 FileOutputStream 將不起作用,將引發(fā)異常。


因此,您將看到此錯(cuò)誤消息:D:/New file/NewFileToGenerateUser26/2018 20:00:14.xls (Is a directory)


可能的修復(fù):


先創(chuàng)建父目錄,然后在不同的語(yǔ)句中創(chuàng)建父目錄后創(chuàng)建您要?jiǎng)?chuàng)建的文件。如:


File file = new File("parent1/parent2");

file.mkDirs();


File desiredFile = new File("parent1/parent2/desiredfile.extensionhere");

desiredFile.createNewFile();


查看完整回答
反對(duì) 回復(fù) 2021-12-10
?
海綿寶寶撒

TA貢獻(xiàn)1809條經(jīng)驗(yàn) 獲得超8個(gè)贊

正如 BrokenEarth 所說(shuō),您已經(jīng)使用要?jiǎng)?chuàng)建的文件的名稱創(chuàng)建了一個(gè)目錄。所以你應(yīng)該分兩步進(jìn)行:

  1. 創(chuàng)建目標(biāo)目錄

  2. 在目錄中創(chuàng)建文件

要做這樣的事情,你可以做這樣的事情:

String filePath = "D:" + File.separator + "someDir";

File dir = new File(filePath);

if (dir.exists() || dir.mkdirs()) {

    // assuming that resultFileName contains the absolute file name, including the directory in which it should go

    File destFile = new File(resultFileName);

    if (destFile.exists() || destFile.createNewFile()) {

        FileOutputStream fos = new FileOutputStream(destFile);

        // ...

    }

}


查看完整回答
反對(duì) 回復(fù) 2021-12-10
?
RISEBY

TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超5個(gè)贊

您的文件正在創(chuàng)建為目錄我已修復(fù)您的代碼并添加了注釋


    File root = new File(filePath);

    //Check if root exists if not create it

    if(!root.exists()) root.mkdirs();

    String resultFileName = "NewFileToGenerate" +getName+headerDate+ ".xls";

    File xlsFile = new File(root, resultFileName);

    //check if xls File exists if not create it

    if(!xlsFile.exists()) {

        try {

            xlsFile.createNewFile();

        } catch (IOException e) {

            e.printStackTrace();

        }

    }


查看完整回答
反對(duì) 回復(fù) 2021-12-10
  • 3 回答
  • 0 關(guān)注
  • 352 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)