Write plugins from a ArrayList to a file?

Discussion in 'Plugin Development' started by RobotA69, Jun 15, 2012.

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

    RobotA69

    I have one of two questions.

    One. I know how to make a file (code here), but how do I put it in Server/plugins/pluginName/fileHere.txt

    Two. How do I read from a list of players and add that to the file?

    Thanks in advance, Robot!
     
  2. use an FileWriter
     
  3. Offline

    theguynextdoor

    In your main class you can call the method getDataFolder() which will lead to your plugins folder so you just need to do

    new File(getDataFolder, "file.txt");

    I'm going to give you a chance to help yourself. Research these 2 things

    * foreach loop (also known as a foreach loop) - this will be used to go through each element of the list
    * BufferedWriter writer = new BufferedWriter(new FileWriter(file, true)); - key thing being buffered writer
     
    RobotA69 likes this.
  4. Offline

    m1enkrafft_man

    This. Just a few tips; remember to CLOSE THE FILE when you're done writing to it, or else the changes won't save properly...
     
  5. Offline

    theguynextdoor

    Don't know about closing the file ... but closing the BufferedWriter is important.


    In your case you probably don't open many files. If you do, and you don't close them, your operating system will be unable to open any more files after a while. Also, while your BufferedWriter has not been garbage collected, the file it was writing to will remain open as far as your operating system is concerned. If that's Windows, you will not be able to delete the file until the file handle is closed, either by the BufferedWriter finally being garbage collected or by the JVM exiting. (http://www.coderanch.com/t/550351/Streams/java/Why-must-we-close-BufferedWriter)
     
    ferrybig likes this.
  6. Offline

    m1enkrafft_man

    That's what I meant. Garsh :(
     
  7. Offline

    theguynextdoor

    I'm sorry, please don't eat me
     
  8. Offline

    RobotA69

    This guy always have the answers :p!
     
Thread Status:
Not open for further replies.

Share This Page