【问题标题】:java.io.FileNotFoundException: (Read-only file system) Macjava.io.FileNotFoundException: (只读文件系统) Mac
【发布时间】:2020-04-26 17:32:40
【问题描述】:

我有一个 SpringBoot 应用程序,我试图在其中测试条形码的生成,但我收到此错误 java.io.FileNotFoundException: (Read-only file system) 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>

Test Class

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();
        }
    }
}

Error

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

【问题讨论】:

  • 您确定路径正确吗?不应该是./test.JPG。此外,默认情况下,应用程序将以当前用户身份运行。试试touch /hello会报错

标签: java spring file barcode fileoutputstream


【解决方案1】:

问题是这样的:

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

注意“/”是根目录。

Mac OS 上的根目录显然位于只读文件系统中。这意味着你不能写它。

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

关于如何完成这项工作的任何想法。

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

【讨论】:

    【解决方案2】:

    写入根目录“/”不是一个好主意。最佳做法是写入主目录,因为默认情况下您拥有主目录的所有权限:

    File outputFile = new File(System.getProperty("user.home"), "test.JPG");
    

    避免文件分隔符

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-22
      • 2015-01-02
      • 2014-04-19
      • 2019-09-30
      • 2012-12-08
      • 1970-01-01
      • 1970-01-01
      • 2012-03-11
      相关资源
      最近更新 更多