Reading/Writing FileNotFoundException(访问被拒绝)

Reading/Writing FileNotFoundException (Access is denied)

每次我的程序尝试读取或写入文件时,我都会收到以下消息:

[System]: LoadConfig >> Config file is missing or corrupt!
C:\Users\Cardinal System\AppData\Roaming\OrlowBase\Run.config (Access is denied)

我在网上查了一下,遇到这个问题的都是简单的读目录,写文件也是一样的。我仔细检查了所有内容,但它继续 throwexception。 我以管理员身份尝试 运行 该程序,但没有任何作用:/

如果您查看我的代码,您会发现我正在记录所有内容。我会提供一份我的日志副本。

这是我的阅读方法:

public static ArrayList<String> readConfig(){

    ArrayList<String> list = new ArrayList<String>();

    try {
        Scanner sc = new Scanner(searchForConfig(baseDir));
        //searchForConfig(baseDir) always will return a file.

        while(sc.hasNextLine()){
            list.add(sc.nextLine());
        }

        sc.close();

        return list;
    } catch (FileNotFoundException e) {
        Logger.addElement("[System]: LoadConfig >> Config file is missing or corrupt!");
        Logger.addElement(e.getMessage());
    }

    list.add("Nothing");

    return list;
}

我的写法是这样的:

public static boolean createNewFile(String Name, String Path, ArrayList<String> Content, boolean override){

    File file2 = new File(Path + dash + Name);

    if(override == false & file2.exists()){

        Name += "1";

    }else{
        if(override == true & file2.exists()){
            file2.delete();
        }
    }

    if(!(Name.indexOf(".") > 0)){
        Name += ".cardinal";
    }

    File file = new File(Path);
    if(!file.isDirectory() | !file.exists()){
        Logger.addElement("[FileWriter]: NewFile >> New file path is invalid.");
        Logger.addElement("[FileWriter]: NewFile >> Creating dirs at path.");
        file.mkdirs();
    }


    try {

        PrintWriter pw = new PrintWriter(Path + dash + Name, "UTF-8");

        for(int i = 0; i < Content.size(); i++){
            pw.println(Content.get(i));
        }
        Logger.addElement("[FileWriter]: NewFile >> Creating file " + Name + " at " + Path + dash);
        pw.close();
        return true;

    } catch (FileNotFoundException e) {
        Logger.addElement("[FileWriter]: NewFile >> FileNotFoundException.");
        Logger.addElement(e.getMessage());
        return false;
    } catch (UnsupportedEncodingException e) {
        Logger.addElement("[FileWriter]: NewFile >> UnsupportedEncodingException.");
        Logger.addElement(e.getMessage());
        return false;
    }
}

更新: 我将记录器更改为记录 stacktraces 并获得了更多信息。有点长,但该​​有的都有:

[System]: Config >> Searching for config in base directory...
[System]: LoadConfig >> Config file is missing or corrupt!
java.io.FileNotFoundException: C:\Users\Cardinal System\AppData\Roaming\OrlowBase\Run.config (Access is denied)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(Unknown Source)
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.util.Scanner.<init>(Unknown Source)
    at com.orlow.base.FileSystem.Config.LoadConfig.readConfig(LoadConfig.java:21)
    at com.orlow.base.FileSystem.Config.Config.<clinit>(Config.java:9)
    at com.orlow.base.Init.Init.preInit(Init.java:20)
    at com.orlow.base.Main.main(Main.java:10)
[System]: Config >> Searching for config in base directory...
[System]: LoadConfig >> Config file is missing or corrupt!
java.io.FileNotFoundException: C:\Users\Cardinal System\AppData\Roaming\OrlowBase\Run.config (Access is denied)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(Unknown Source)
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.util.Scanner.<init>(Unknown Source)
    at com.orlow.base.FileSystem.Config.LoadConfig.readConfig(LoadConfig.java:21)
    at com.orlow.base.FileSystem.Config.Config.checkConfig(Config.java:13)
    at com.orlow.base.Init.Init.preInit(Init.java:20)
    at com.orlow.base.Main.main(Main.java:10)
[preInit]: Logger >> Starting logger...
[preInit]: Tests >> Testing config...
[System]: Config >> Searching for config in base directory...
[System]: LoadConfig >> Config file is missing or corrupt!
java.io.FileNotFoundException: C:\Users\Cardinal System\AppData\Roaming\OrlowBase\Run.config (Access is denied)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(Unknown Source)
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.util.Scanner.<init>(Unknown Source)
    at com.orlow.base.FileSystem.Config.LoadConfig.readConfig(LoadConfig.java:21)
    at com.orlow.base.Init.Init.preTests(Init.java:62)
    at com.orlow.base.Init.Init.preInit(Init.java:22)
    at com.orlow.base.Main.main(Main.java:10)
[preInit]: Config >> Config file is corrupt.
[System]: SaveConfig >> Replacing Config file...
java.io.FileNotFoundException: C:\Users\Cardinal System\AppData\Roaming\OrlowBase\Run.config (Access is denied)
    at java.io.FileOutputStream.open0(Native Method)
    at java.io.FileOutputStream.open(Unknown Source)
    at java.io.FileOutputStream.<init>(Unknown Source)
    at java.io.FileOutputStream.<init>(Unknown Source)
    at java.io.PrintWriter.<init>(Unknown Source)
    at java.io.PrintWriter.<init>(Unknown Source)
    at com.orlow.base.FileSystem.DataStorage.Files.createNewFile(Files.java:98)
    at com.orlow.base.FileSystem.Config.SaveConfig.replaceCorruptConfig(SaveConfig.java:26)
    at com.orlow.base.FileSystem.Config.Config.getConfigMode(Config.java:32)
    at com.orlow.base.FileSystem.Config.Config.getBuildPath(Config.java:77)
    at com.orlow.base.Init.Init.preTests(Init.java:68)
    at com.orlow.base.Init.Init.preInit(Init.java:22)
    at com.orlow.base.Main.main(Main.java:10)
[preInit]: Config >> Config file is corrupt.
[System]: SaveConfig >> Replacing Config file...
java.io.FileNotFoundException: C:\Users\Cardinal System\AppData\Roaming\OrlowBase\Run.config (Access is denied)
    at java.io.FileOutputStream.open0(Native Method)
    at java.io.FileOutputStream.open(Unknown Source)
    at java.io.FileOutputStream.<init>(Unknown Source)
    at java.io.FileOutputStream.<init>(Unknown Source)
    at java.io.PrintWriter.<init>(Unknown Source)
    at java.io.PrintWriter.<init>(Unknown Source)
    at com.orlow.base.FileSystem.DataStorage.Files.createNewFile(Files.java:98)
    at com.orlow.base.FileSystem.Config.SaveConfig.replaceCorruptConfig(SaveConfig.java:26)
    at com.orlow.base.FileSystem.Config.Config.getConfigMode(Config.java:32)
    at com.orlow.base.Init.Init.preTests(Init.java:74)
    at com.orlow.base.Init.Init.preInit(Init.java:22)
    at com.orlow.base.Main.main(Main.java:10)
[preInit]: Config >> Config file is corrupt.
[System]: SaveConfig >> Replacing Config file...
java.io.FileNotFoundException: C:\Users\Cardinal System\AppData\Roaming\OrlowBase\Run.config (Access is denied)
    at java.io.FileOutputStream.open0(Native Method)
    at java.io.FileOutputStream.open(Unknown Source)
    at java.io.FileOutputStream.<init>(Unknown Source)
    at java.io.FileOutputStream.<init>(Unknown Source)
    at java.io.PrintWriter.<init>(Unknown Source)
    at java.io.PrintWriter.<init>(Unknown Source)
    at com.orlow.base.FileSystem.DataStorage.Files.createNewFile(Files.java:98)
    at com.orlow.base.FileSystem.Config.SaveConfig.replaceCorruptConfig(SaveConfig.java:26)
    at com.orlow.base.FileSystem.Config.Config.getConfigMode(Config.java:32)
    at com.orlow.base.FileSystem.Config.Config.getLogPath(Config.java:126)
    at com.orlow.base.Init.Init.preTests(Init.java:80)
    at com.orlow.base.Init.Init.preInit(Init.java:22)
    at com.orlow.base.Main.main(Main.java:10)
[preInit]: Config >> Log path is set to C:\Users\Cardinal System\AppData\Roaming\OrlowBase\Logs
[preInit]: Tests >> Config >> Config test was successful.
[preInit] Tests >> Testing file writer...
[FileWriter]: NewFile >> Creating file firstTestFile.cardinal at C:\Users\Cardinal System\AppData\Roaming\OrlowBase\Data\
[FileWriter]: NewFile >> Creating file secondTextFile.cardinal at C:\Users\Cardinal System\AppData\Roaming\OrlowBase\Data\
[FileWriter]: NewFile >> Creating file thirdTestFile.cardinal at C:\Users\Cardinal System\Desktop\Peter\Programming\Java\Eclipse\Workspaces\Main Workspace\TestSkills\
[FileWriter]: NewFile >> Creating file forthTestFile.cardinal at C:\Users\Cardinal System\Desktop\Peter\Programming\Java\Eclipse\Workspaces\Main Workspace\TestSkills\
[preInit] Tests >> File writer test was successful.
[Init]: Console >> Rendering console GUI...
[System]: Console >> Type "?" for help.
  • 这可能是权限问题。作为什么用户,您是 运行 应用程序? 'Cardinal System' 以外的某个用户?检查 read/write 对 C:\Users\Cardinal System\AppData\Roaming\OrlowBase\*
  • 的访问权限
  • 在捕获 FileNotFoundException 时不要吞下堆栈跟踪。它可能会引导您找到根本原因。

更新: 'AppData' 是用户特定的文件夹,只能由用户和管理员访问。在此处查看更多信息: Can any user access the %APPDATA% folder

检查用户对 Orlowbase 文件夹的权限。 当我拒绝 read/write/total 控制的特权时,我得到了你显示的异常,当我允许它们时,你的程序运行正常。