Solved Dynamically named files?

Discussion in 'Plugin Development' started by zack6849, Oct 5, 2012.

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

    zack6849

    Hey guys, im trying to add a feature to one of my plugins to make a new log file each day, however i cant seem to wrap my head around how i would do this, as the only method i know of is to save a resource from the jar to the plugins datafolder. I highly doubt the way to go about this is to make a empty log in the jar for every day in the forseeable future. any help would be greatly appreciated!

    However im not the sharpest crayon in the box when it comes to files so if you could give a breif description of how to do this in basic terms it would be nice :)
     
  2. Offline

    LukeSFT

    I think you can copy the old log with another name to the same location and delete the old
     
  3. Offline

    zack6849

    i dont wish to delete the old one. and info on how to do this would be appreciated.
     
  4. Offline

    Cjreek

    Maybe I misunderstood something but can't you just add the current date to the log filename? (log2012-03-10.log)
     
  5. Offline

    LukeSFT

    use:
    Code:
    File file_old = new File("oldfile");
    File file_new = new File("newfile");
    file_old.renameTo(file_new);
    
    this would change the name of the file named oldfile to newfile.
     
  6. Offline

    kroltan

    If you want things like log_9-11-2001.txt (I can't remember what, but something happened this date), Check out Java's Date class, then get the day/month/year or whatever you want, and create a file concatenating those infos.
     
  7. Offline

    zack6849

    i already plan on doing this but the problem is creating the file itself.
    I usually just use saveRescource()
     
  8. Offline

    kroltan

    Hint: java.io
    Or you can search "how to write files in Java" in Google.
    EDIT: Found this code in the 1st result of the above Google search:
    Code:java
    1. import java.io.*;
    2. class FileWrite {
    3. public static void main(String args[]) {
    4. try {
    5. // Create file
    6. FileWriter fstream = new FileWriter("out.txt"); // The parameter is the destination of your file
    7. BufferedWriter out = new BufferedWriter(fstream); // This is the thing that will write your contents
    8. out.write("Hello Java"); // Write whatever you want. For newlines, use \n
    9. //Close the output stream
    10. out.close();
    11. } catch (Exception e) {//Catch exception if any
    12. System.err.println("Error: " + e.getMessage());
    13. }
    14. }
    15. }
     
  9. Offline

    zack6849

    even with that method it requires there to be a file with that name in the jar...
     
  10. Offline

    Courier

    No; it doesn't.
     
  11. Offline

    kroltan

    Java's IO package isn't constrained by the Bukkit API. You can do whatever you want with it, as long as it fits in the submission guidelines (e.g. no deleting system32, lol)
     
  12. Offline

    zack6849

    i just copied that exact code and it threw an error because it didn't find the file, but i fixed it myself.
     
  13. Offline

    kroltan

    Ofcourse, out.txt isn't anywhere. You need a valid path.
     
Thread Status:
Not open for further replies.

Share This Page