Java中的权限被拒绝

Java中的权限被拒绝

本文介绍了java.io.IOException:Java中的权限被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的项目的同一文件夹中创建一个文件,但是我无法动态创建该文件.我正在尝试:

I am trying to create a file into the same folder in my project, but I am not able to create that file dynamically. I am trying this:

try {
            System.out.println("path"+System.getProperty("user.dir"));
            File file = new File("/textfile.txt");
            file.createNewFile();
            //file.createNewFile();
        } catch (Exception e) {
            e.printStackTrace();
        }

我得到的错误是这样的:

What I am getting error is this:

java.io.IOException: Permission denied

任何建议都会受到欢迎.

Any suggestion will be welcomed.

推荐答案

要将文件创建到项目中的同一文件夹中,您的路径必须是相对的.

To create a File into the same folder in your project, your path has to be relative.

您给出的路径是绝对路径,因为它是从/开始的.为了使您的路径相对,请从路径中删除/并尝试执行以下操作:

The path that you are giving is absolute, because it is starting from /. For your path to be relative, remove / from the path and try this :

File file = new File("textfile.txt");

这篇关于java.io.IOException:Java中的权限被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 21:54