Simple file output. Just text being written, no reading and such. Called on an event.

Discussion in 'Plugin Development' started by James | Buchanan, Feb 16, 2012.

Thread Status:
Not open for further replies.
  1. Hello forums,

    I was looking for a way to print, and only print, to a file using the onPlayerBreakEvent();
    This is the kind of output im hoping for:

    Player:<playername> destroyed block at <X,Y,Z>

    Any help will be appreciated :D

    anybody?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 24, 2016
  2. Offline

    V10lator

    <Edit by Moderator: Redacted bit url>
     
    Last edited by a moderator: Feb 20, 2017
  3. Offline

    hatstand

    I assume you're after a plugin-specific log file of sorts, for which, you'd need to initialise a new file, and create it if it doesn't exist:
    Code:
    File logFile = new File(getDataFolder() + File.Separator + "fileName.log");
     
    if(!logFile.exists())
    {
        try
        {
            logFile.createNewFile();
        }
        catch(IOException e)
        {
            e.printStackTrace
        }
    }
    You then need to initialise a new fileWriter, using the logFile, and telling it to append instead of wipe the file:
    Code:
    FileWriter logFileWriter;
     
    if(logFile.exists())
    {
        try
        {
            logFileWriter = new FileWriter(logFile, true);
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
    }
    Here, we also check that creating the file was successful - If it was, the file exists, if not, it doesn't, so we don't attempt to initialise the fileWriter.

    You can then write lines to the file like so (Always write a newline, else you'll end up with everything on one line):
    Code:
    if(logFileWriter != null)
    {
        logFileWriter.write("stuff here\n");
    }
    Always nice to check if you can write before attempting to - If the fileWriter is null, that means initialising it errored out, or the file wasn't created successfully, and you shouldn't attempt to write to it.

    Then, when your plugin is shutting down (in your onDisable), you need to flush & close the fileWriter (Flushing it writes any unwritten data to the file):
    Code:
    logFileWriter.flush();
    logFileWriter.close();
    [quote uid=92350 name="V10lator" post=969060]<Edit by Moderator: Redacted bit url>
    Not helpful.
     
    Last edited by a moderator: Feb 20, 2017
  4. Offline

    V10lator

    If I click on that link and then on the first link I got this:
    Code:
    try {
        BufferedWriter out = new BufferedWriter(new FileWriter("outfilename"));
        out.write("aString");
        out.close();
    } catch (IOException e) {
    }
    With a description what it does and more examples...
    And you call that not helpfull?
     
  5. Offline

    hatstand

    It's a condescending reply, and this is a perfectly valid place for him to ask such a question.
     
  6. 2 things:

    - Is the file placed in the plugin's folder?
    - Can that code be called in a method?
     
  7. Offline

    V10lator

    No. At best you use
    BufferedWriter out = new BufferedWriter(new FileWriter(new File(plugin.getDataFolder(), "outfilename")));
    Yes
     
Thread Status:
Not open for further replies.

Share This Page