gpt4 book ai didi

java.io.FileNotFoundException : (Read-only file system) Mac

转载 作者:行者123 更新时间:2023-12-02 09:07:53 26 4
gpt4 key购买 nike

我有一个 SpringBoot 应用程序,我试图在其中测试条形码的生成,但收到此错误 java.io.FileNotFoundException: (只读文件系统) Mac

以下是完成此任务的代码:

pom.xml

        <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.sf.barcode4j</groupId>
<artifactId>barcode4j</artifactId>
<version>2.1</version>
</dependency>

测试类

public class FooTest extends TestCase {
@Test
public void testP() {
try {
Code128Bean bean = new Code128Bean();
final int dpi = 160;

//Configure the barcode generator
bean.setModuleWidth(UnitConv.in2mm(2.8f / dpi));

bean.doQuietZone(false);

//Open output file
File outputFile = new File("/" + "test" + ".JPG");

FileOutputStream out = new FileOutputStream(outputFile);

BitmapCanvasProvider canvas = new BitmapCanvasProvider(
out, "image/x-png", dpi, BufferedImage.TYPE_BYTE_BINARY, false, 0);

//Generate the barcode
bean.generateBarcode(canvas, "test");

//Signal end of generation
canvas.finish();

System.out.println("Bar Code is generated successfully…");
}
catch (IOException ex) {
ex.printStackTrace();
}
}
}

错误

java.io.FileNotFoundException: /test.JPG (Read-only file system)
at java.io.FileOutputStream.open0(Native Method)
at java.io.FileOutputStream.open(FileOutputStream.java:270)
at java.io.FileOutputStream.<init>(FileOutputStream.java:213)
at java.io.FileOutputStream.<init>(FileOutputStream.java:162)

关于如何在我的机器(MacBook)上实现此功能,有什么想法吗? Linux 的配​​置会有所不同吗?

最佳答案

问题是这样的:

File outputFile = new File("/" + "test" + ".JPG");

注意“/”是根目录。

Mac OS 上的根目录显然位于只读文件系统中。这意味着您无法写入它。

在 Linux/UNIX 系统上,根文件系统通常不是只读的,但您的应用程序无论如何都没有写入根目录的权限。

Any ideas on how I could make this work.

不要尝试将文件写入根目录“/”。寻找更合适的地方;例如当前工作目录、用户主目录、临时目录等。

关于java.io.FileNotFoundException : (Read-only file system) Mac,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59658422/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com