ImageIO.write()无法保存文件。

huangapple go评论146阅读模式
英文:

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 = 30000Term = 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);

huangapple
  • 本文由 发表于 2020年6月29日 11:11:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/62630648.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定