FileOutputStream 抛出 FileNotFoundException
FileOutputStream throws FileNotFoundException
我正在尝试创建一个新文件和目录,但是当我调用 "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();
}
这里我有文件路径,但在该文件路径中,我必须通过在其中附加日期来创建另一个文件夹,然后我必须保存文件。
这里是 stackTrace
D:/New file/NewFileToGenerateUser26/2018 20:00:14.xls(是目录)
异常的名称具有误导性,但源于无法写入/创建该文件。
原因是您在文件名中使用了 /
,这是一个路径分隔符。甚至Windows及其\
也保持Posix标准,不允许
当您使用
file.makeDirs();
它创建了所有不存在的目录,包括 "NewFileToGenerate" +getName+headerDate+ ".xls"
。是的,您要创建的文件已创建为目录。
然后您调用了 file.createNewFile(),这将 return 错误,因为存在与该文件同名的目录。
尝试对目录使用 FileOutputStream 将不起作用,将抛出异常。
因此,您将看到此错误消息:
D:/New file/NewFileToGenerateUser26/2018 20:00:14.xls (是目录)
可能的修复:
先创建父目录,在不同的语句创建父目录后,再创建你想创建的文件。
如:
File file = new File("parent1/parent2");
file.mkDirs();
File desiredFile = new File("parent1/parent2/desiredfile.extensionhere");
desiredFile.createNewFile();
您的文件正在创建为目录我已经修复了您的代码并添加了注释
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();
}
}
正如 BrokenEarth 所说,您已经创建了一个目录,其中包含您要创建的文件的名称。所以你应该分两步进行:
- 创建目标目录
- 在目录中创建文件
要做这样的事情,你可以这样做:
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);
// ...
}
}
我正在尝试创建一个新文件和目录,但是当我调用 "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();
}
这里我有文件路径,但在该文件路径中,我必须通过在其中附加日期来创建另一个文件夹,然后我必须保存文件。
这里是 stackTrace
D:/New file/NewFileToGenerateUser26/2018 20:00:14.xls(是目录)
异常的名称具有误导性,但源于无法写入/创建该文件。
原因是您在文件名中使用了 /
,这是一个路径分隔符。甚至Windows及其\
也保持Posix标准,不允许
当您使用
file.makeDirs();
它创建了所有不存在的目录,包括 "NewFileToGenerate" +getName+headerDate+ ".xls"
。是的,您要创建的文件已创建为目录。
然后您调用了 file.createNewFile(),这将 return 错误,因为存在与该文件同名的目录。
尝试对目录使用 FileOutputStream 将不起作用,将抛出异常。
因此,您将看到此错误消息: D:/New file/NewFileToGenerateUser26/2018 20:00:14.xls (是目录)
可能的修复:
先创建父目录,在不同的语句创建父目录后,再创建你想创建的文件。 如:
File file = new File("parent1/parent2");
file.mkDirs();
File desiredFile = new File("parent1/parent2/desiredfile.extensionhere");
desiredFile.createNewFile();
您的文件正在创建为目录我已经修复了您的代码并添加了注释
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();
}
}
正如 BrokenEarth 所说,您已经创建了一个目录,其中包含您要创建的文件的名称。所以你应该分两步进行:
- 创建目标目录
- 在目录中创建文件
要做这样的事情,你可以这样做:
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);
// ...
}
}