【问题标题】:FileOutputStream throws FileNotFoundExceptionFileOutputStream 抛出 FileNotFoundException
【发布时间】:2018-10-26 14:24:49
【问题描述】:

我正在尝试与目录一起创建一个新文件,但是当我调用“fos = new FileOutputStream(file);”时它总是抛出文件未找到错误。 这是代码

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

这里我有文件路径,但在该文件路径中,我必须通过在其中添加日期来创建另一个文件夹,然后我必须保存文件。

这里是堆栈跟踪

D:/New File/NewFileToGenerateUser26/2018 20:00:14.xls(是目录)

【问题讨论】:

  • 请您也发布堆栈跟踪吗?了解正在发生的事情通常很有帮助;)
  • 如果文件引用的是目录而不是常规文件,则会发生这种情况。
  • @Alexandre 使用堆栈跟踪进行编辑

标签: java spring file java-io


【解决方案1】:

当你使用时

file.makeDirs();

它创建了所有不存在的目录,包括"NewFileToGenerate" +getName+headerDate+ ".xls"。是的,您要创建的文件是作为目录创建的。

然后你调用了file.createNewFile(),它会返回false,因为存在与文件同名的目录。

尝试对目录使用 FileOutputStream 将不起作用,将引发异常。

因此,您将看到以下错误消息: D:/新建文件/NewFileToGenerateUser26/2018 20:00:14.xls(是目录)

可能的修复:

先创建父目录,然后在不同的语句中创建父目录后创建你想要创建的文件。 如:

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

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

【讨论】:

  • 但是为什么我没有创建任何目录或文件
  • 应该是……再检查一下
  • 是的,它正在使用 .xls 名称创建目录正在创建如何创建一个新文件夹和一个新目录以及日期。
  • @HarshKumrawat 使用"MM_dd_yyyy-HH_mm_ss" 作为格式。
  • @JoopEggen 我也在找这个东西。太好了,这有帮助
【解决方案2】:

正如 BrokenEarth 所说,您已经创建了一个目录,其中包含您要创建的文件的名称。所以你应该分两步进行:

  1. 创建目标目录
  2. 在目录中创建文件

要做这样的事情,你可以这样做:

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);
        // ...
    }
}

【讨论】:

  • 是否应该检查目录是否已经存在,类似于我的答案?
  • @Rab: 是的,因为 mkdirs() 将返回 false 是目录已经存在 - 我添加了检查
【解决方案3】:

异常的名称具有误导性,但源于无法写入/创建该文件。

原因是您在文件名中使用了/,这是一个路径分隔符。即使是带有\ 的 Windows 也维护 Posix 标准并且不允许它。

【讨论】:

  • Java 在 Windows 上将 / 转换为 \。
  • @PeterLawrey 在 cmd.exe 中也可以使用/ (POSIX)。那么java也这样做吗?这与我在 Java 方面有多年的经验有关。一个人学习
【解决方案4】:

你的文件被创建为一个目录我已经修复了你的代码并添加了 cmets

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-08
    • 2019-09-17
    • 2016-04-05
    • 1970-01-01
    • 2019-03-08
    • 2015-11-06
    • 2013-10-24
    • 1970-01-01
    相关资源
    最近更新 更多