英文:
ImageIO.write() not saving file
问题
绝对文件路径似乎正在正确形成,但文件未被写入。
代码:
var image = ImageIO.read(new ByteArrayInputStream(attachPageScreenshot()));
var saveDirectory = Paths.get("target", "screenshots").toAbsolutePath().toString();
var builder = new StringBuilder();
builder.append("\\").append(context.getDisplayName()).append(".png");
var filePath = saveDirectory.concat(builder.toString());
var saveFile = new File(filePath);
ImageIO.write(image, "png", saveFile);
输出:
java.io.FileNotFoundException: E:\workspace\java\selenium-junit5-starter\target\screenshots\Verify Total Interest Per Annum - Deposit = 30000,Term = 2 Years.png(系统找不到指定的路径)
有什么问题吗?
英文:
The absolute file path seems to be forming correctly but the file is not being written.
Code:
var image = ImageIO.read(new ByteArrayInputStream(attachPageScreenshot()));
var saveDirectory = Paths.get("target", "screenshots").toAbsolutePath().toString();
var builder = new StringBuilder();
builder.append("\\").append(context.getDisplayName()).append(".png");
var filePath = saveDirectory.concat(builder.toString());
var saveFile = new File(filePath);
ImageIO.write(image, "png", saveFile);
Output:
java.io.FileNotFoundException: E:\workspace\java\selenium-junit5-starter\target\screenshots\Verify Total Interest Per Annum - Deposit = 30000, Term = 2 Years.png (The system cannot find the path specified)
Anything amiss?
答案1
得分: 1
我假设 Paths.get()
会在目录不存在的情况下自动创建该目录。
var dir = new File(saveDirectory);
if (!dir.exists()) {
dir.mkdirs();
}
ImageIO.write(image, "png", saveFile);
英文:
I assumed Paths.get()
automatically created the directory if it did not exist.
var dir = new File(saveDirectory);
if (!dir.exists()) {
dir.mkdirs();
}
ImageIO.write(image, "png", saveFile);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论