Search code examples
javajava.util.scannerfilenotfoundexceptionaccess-deniedprintwriter

Reading/Writing FileNotFoundException (Access is denied)


Every time my program tries to read or write a file, I get the following message:

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

I looked this up on the internet, and everyone that had this issue was simply trying to read a directory, it was the same way for writing files. I double checked everything, but it continues to throw an exception. I tried running the program as administrator, but that didn't do anything :/

If you look at my code, you can see I'm logging everything. I will provide a copy of my log.

This is my reading method:

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

My writing method is the following:

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

UPDATE: I changed my logger to record stacktraces and got more info. It's a bit lengthy, but everything is there:

[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.

Solution

  • Check the privileges for the user on the Orlowbase folder. When I deny the privileges for read/write/total control I got the exception you show, when I allow them, your program runs fine.

    enter image description here