0

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.
Cardinal System
  • 2,749
  • 3
  • 21
  • 42
  • maybe change `&` to `&&` and `|` to `||` – Scary Wombat Apr 27 '17 at 00:15
  • and change `if(!file.isDirectory() | !file.exists()){` to `if(!file.isDirectory() && !file.exists()){` – Scary Wombat Apr 27 '17 at 00:16
  • in other words debug your code – Scary Wombat Apr 27 '17 at 00:17
  • are you executing as an run as administrator. Right click the `OrlowBase` folder and in security tab give this folder as a write-permission. – Rajith Pemabandu Apr 27 '17 at 00:20
  • @RajithPemabandu I ran the program as administrator, so that shouldn't be the issue... Also, I want this to work on computers other than my own, so changing folder permissions isn't an option :/ – Cardinal System Apr 27 '17 at 00:49
  • @ScaryWombat What is the difference? I'll try next time I can. – Cardinal System Apr 27 '17 at 00:50
  • Instead of cropping the top off the stack trace please post the whole thing, *including* the class and message. Impossible to follow otherwise. – user207421 Apr 29 '17 at 00:41
  • @EJP Sorry about that, seems like `e.getStackTrace();` doesn't work. This would be a lot easier if people would answer my [other question](http://stackoverflow.com/questions/43623922/redirect-console-to-printstream-and-system-out)... I fixed it. – Cardinal System Apr 29 '17 at 00:50
  • Thank you. Can I now note that you shouldn't provide your own error messages: you should use the one in the exception. The file is neither missing nor corrupt. – user207421 Apr 29 '17 at 00:53
  • @EJP I do know that... That error msg is only supposed to be displayed if the file is actually missing. It is for when the program is finished. But because it can't read the file, it thinks it is missing. – Cardinal System Apr 29 '17 at 00:56
  • This could be due to the file is already locked by another thread. Can try using FileLock to aquire the lock before use which blocks until done and release after use. Please see http://stackoverflow.com/questions/128038/how-can-i-lock-a-file-using-java-if-possible for usage. – OTM May 03 '17 at 15:46

2 Answers2

1

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

sirandy
  • 1,834
  • 5
  • 27
  • 32
  • Although I was able to resolve this issue without changing the folder permissions, you still put the most effort into getting a good answer, so you get the bounty :) – Cardinal System May 03 '17 at 23:27
-1
  • It might be a permission issue. As what user you are running the application? Some user other than 'Cardinal System'? Check read/write access to C:\Users\Cardinal System\AppData\Roaming\OrlowBase\*
  • Don't swallow stacktrace while catching FileNotFoundException. It might lead you to underlying cause.

UPDATE: 'AppData' is user specific folder and only accessible by the user and administrator. See more information here: Can any user access the %APPDATA% folder

Community
  • 1
  • 1
VinPro
  • 882
  • 1
  • 6
  • 10
  • I ran the program as administrator, so that shouldn't be the issue... Also, I want this to work on computers other than my own, so changing folder permissions isn't an option :/ about AppData, Minecraft is java based an has access to AppData, despite its lack of elevated privileges – Cardinal System Apr 27 '17 at 00:52
  • 1
    I tested createNewFile() method it creates file without error on my machine. Can you share code for searchForConfig() method? And what's "basedir" path? Your log does not have any Path/dir related information for this method! – VinPro Apr 28 '17 at 01:05