读/写FileNotFoundException(访问被拒绝)

时间:2017-04-27 00:07:20

标签: java java.util.scanner filenotfoundexception access-denied printwriter

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

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

我在互联网上看了这个,每个遇到这个问题的人都只是想读一个目录,这与写文件的方式相同。我仔细检查了所有内容,但仍然是throw exception。 我尝试以管理员身份运行该程序,但是没有做任何事情:/

如果查看我的代码,您可以看到我记录了所有内容。我将提供我的日志副本。

这是我的阅读方法:

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.

2 个答案:

答案 0 :(得分:1)

检查Orlowbase文件夹上用户的权限。 当我拒绝读取/写入/总控制的权限时,我得到了您显示的异常,当我允许它们时,您的程序运行正常。

enter image description here

答案 1 :(得分:-1)

  • 这可能是许可问题。您正在运行该应用程序的用户是什么?除了“红衣主教系统”以外的一些用户?检查对C:\Users\Cardinal System\AppData\Roaming\OrlowBase\*
  • 的读/写访问权限
  • 捕获FileNotFoundException时不要吞下stacktrace。它可能会引导你找到根本原因。

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