Solved Logging to a file

Discussion in 'Plugin Development' started by Lulonaut, Aug 31, 2020.

Thread Status:
Not open for further replies.
  1. Offline

    Lulonaut

    I followed this Guide on how to make a Log file.

    At this line:

    Code:
    File dataFolder = getDataFolder();
    getDataFolder is getting marked as an Error in Intellij.

    with this Error:

    Code:
    Cannot resolve method 'getDataFolder' in 'Listeners'
    and with these Suggestions to solve it:

    [​IMG]


    I already tried it with Bukkit.getDataFolder() but the same Error is shown.

    However it exists in the Docs.

    Im new to Java so i may be missing something obvios, thanks for any help

    For anyone interested: i fixed it myself:

    Full Code (open)


    This will throw an Exception when the Folder doesnt exist, but the file will be created if it doesnt exist.

    All the other Credit goes to this thread.
    Code:
    public void logToFile(String message) {
    
            try
            {
    
                File saveTo = new File("path/to/my/log.txt" );
                if (!saveTo.exists())
                {
                    saveTo.createNewFile();
                }
    
                FileWriter fw = new FileWriter(saveTo, true);
    
                PrintWriter pw = new PrintWriter(fw);
    
                pw.println(message);
    
                pw.flush();
    
                pw.close();
    
            } catch (IOException e)
            {
    
                e.printStackTrace();
    
            }
    
        }
     

    Attached Files:

    Last edited by a moderator: Aug 31, 2020
  2. Offline

    KarimAKL

    @Lulonaut The reason you can’t use getDataFolder() is because you’re trying to call it outside of your main class without an instance of your main class.
     
Thread Status:
Not open for further replies.

Share This Page