[英]java.io.FileNotFoundException: (Access is denied)
我正在尝试在Java项目根目录中动态创建一些文件。 但是运行代码时出现以下错误。
java.io.FileNotFoundException: D:\POS_ALL\T_POS_NEWEST\TouchPosApplication\WebContent\zharaimages\279 (Access is denied)
是否可以在Java中将文件写入根项目文件夹? 这是使用的代码。
private void createImage(PosItemImageDTO imageDTO,String path) throws IOException {
byte[] bytes = imageDTO.getPosItemImage();
path = path + "\\";
if(bytes!=null && bytes.length>0){
OutputStream out = null;
// BufferedImage img = ImageIO.read(new ByteArrayInputStream(bytes));
File l = new File(path);
out = new BufferedOutputStream(new FileOutputStream(path));
out.write(bytes);
if (out != null) {
out.close();
}
}
}
这是因为似乎您正在尝试在此处打开并读取目录。 正如您所说,文件没有指定任何扩展名,因此java会将其作为目录。 使用isFile()
方法在打开之前检查文件。 您可以使用listFiles()
方法获取目录的文件。
请确保path = path + "\\\\";
是一条正确的道路。 如果有目录,程序将显示“ Access is denied
。 您应该在打开文件之前添加一些检查,就像if (l.isDirectory())
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.