java.io.FileNotFoundException:(只读文件系统)Mac [英] java.io.FileNotFoundException: (Read-only file system) Mac

查看:494
本文介绍了java.io.FileNotFoundException:(只读文件系统)Mac的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a SpringBoot app in which I am trying to test generation of barcodes but I am getting this error java.io.FileNotFoundException: (Read-only file system) Mac.

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

Here's the code to accomplish this task:

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的配置会有所不同吗?

Any ideas on how I could make this work on my machine (MacBook)? Would the configuration be different for Linux?

推荐答案

问题是这样的:

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

请注意,"/"是根目录.

Note that "/" is the root directory.

Mac OS上的根目录显然位于只读文件系统中.这意味着您无法对其进行写操作.

The root directory on Mac OS is apparently in a read-only file system. That means you cannot write to it.

在Linux/UNIX系统上,根文件系统通常不是只读的,但是您的应用程序仍然无权写入根目录.

On Linux / UNIX systems, the root filesystem is typically not read-only, but your application won't have permission to write to the root directory anyway.

关于如何进行这项工作的任何想法.

Any ideas on how I could make this work.

不要尝试将文件写入根目录"/".找到更合适的地方;例如当前的工作目录,用户的主目录,临时目录等.

Don't try to write files into the root directory "/". Find somewhere more appropriate; e.g. the current working directory, the user's home directory, a temporary directory, etcetera.

这篇关于java.io.FileNotFoundException:(只读文件系统)Mac的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆