[SOLVED] Generating Text File To Plugin Folder

Discussion in 'Plugin Development' started by SnowGears, Jul 27, 2012.

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

    SnowGears

    All I want is a text file that I write beforehand ( similar to the config.yml ) but just text. Sort of like a seperate readme file that is generated in the same directory as the config file. How would I do this? Thanks!

    Oh and I don't even need to write to the file from the plugin at all. Just a plain text file that I write by hand and is generated in the folder of the plugin next to the config.yml. Thanks in advance!

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

    d0de

    Simply do

    Code:
    public void createYourTxtFile(){
       
        File file = new File(getDataFolder(), "yourtxtfilename.txt");
     
        if (!(file.exists())) {
     
            file.mkdir();
        }
     
    }
     
    Tooner101 likes this.
  3. Offline

    SnowGears

    But where will that read from? Do I just make a "yourtextfilehere.txt" in the same directory as my config.yml?

    EDIT: Also, the code you gave me creates an empty folder with the title of "whatever.txt"
     
  4. Offline

    r0306

    Tooner101
    That doesn't read from a text file. It only checks if that text file exists and creates one if it doesn't exist.

    d0de
    mkdir() will not work for this situation. It makes the directory. It doesn't create the file. If that code was used, Java would create another folder inside your plugin folder named yourtxtfilename.txt.

    To create the file, you would need:

    Code:
    public void createYourTxtFile(){
     
        File file = new File(getDataFolder(), "yourtxtfilename.txt");
     
        if (!(getDataFolder().exists())) {
     
            getDataFolder().mkdir();
        }
        if (!file.isFile()) file.createNewFile();
     
    }
     
    Tooner101 likes this.
  5. Offline

    d0de

    Messed up :p
     
  6. Offline

    SnowGears

    This still only makes a blank text file though. How do I generate the prewritten one that is in the same directory as my config.yml file?
     
  7. [quote uid=90696604 name="Tooner101" post=1237929]This still only makes a blank text file though. How do I generate the prewritten one that is in the same directory as my config.yml file?[/quote]
    You have to write in it... <Edit by Moderator: Redacted bit url>
     
    Last edited by a moderator: Feb 19, 2017
  8. Offline

    SnowGears

    [quote uid=92350 name="V10lator" post=1237939]You have to write in it... <Edit by Moderator: Redacted bit url>
    But Can't I just write it by hand as a .txt file in eclipse and tell it to generate the prewritten one exactly like it does with the config.yml file?
     
    Last edited by a moderator: Feb 19, 2017
  9. Offline

    r0306

    Tooner101
    Where is the stuff that you want to put in the text file? You could make one them zip it up with the JAR file and then copy the file into the directory.
     
  10. Offline

    SnowGears

    How would I do that? I wouldn't want people to have to unpack my jar file to get to the readme.txt
     
  11. Sure, but copying means reading out the one in the jar and writing it to a file on disc... ^^
     
  12. Offline

    SnowGears

    Would that be hard to do? I don't have much experience with this stuff.
     
  13. Offline

    r0306

    Tooner101
    There's no easy way to do this but here's what I do:
    Code:
    public void generateTextFile()
    {
      File file = new File(getDataFolder(), "text.txt");
      //create new file method
      InputStream input = getClass().getResourceAsStream("directory of file inside jar file relative to this class");
      BufferedReader reader = new BufferedReader(new InputStreamReader(input));
     
      FileOutputStream output = new FileOutputStream(file.getPath());
      BufferedWriter writer = new BufferedWriter(output);
     
      String line = "";
     
      while (line = reader.nextLine() != null)
      {
        writer.write(line);
      }
     
      writer.close();
      reader.close();
    }
    Basically, you put the text file somewhere in the same package as the class this method is in and call up the method to generate the file. It copies and pastes the stuff line by line onto the external text file.
     
    Tooner101 likes this.
  14. No, it's pretty simple... Let me search a example...
    Code:java
    1. try
    2. {
    3. themeFile.createNewFile();
    4. }
    5. catch (IOException e)
    6. {
    7. this.plugin.info2log("error: can't create 'plugins/V10lator/themes/" + count + ".sgt'");
    8. e.printStackTrace();
    9. return -1;
    10. }
    11. try
    12. {
    13. InputStream stream=this.getClass().getResourceAsStream("/de/V10lator/SignClock/themes/" + count + ".sgt");
    14. if(stream!=null) {
    15. Writer tmpwriter = new StringWriter();
    16. char[] buffer = new char[1024];
    17. String out = "";
    18. try
    19. {
    20. Reader reader = new BufferedReader(
    21. new InputStreamReader(stream, "UTF-8"));
    22. int n;
    23. while((n = reader.read(buffer)) != -1)
    24. {
    25. tmpwriter.write(buffer, 0, n);
    26. out = out + tmpwriter;
    27. }
    28. FileWriter writer = new FileWriter(themeFile);
    29. writer.write(out);
    30. writer.close();
    31. tmpwriter.close();
    32. }
    33. catch (IOException e)
    34. {
    35. this.plugin.info2log("BUG (IOExpection at 107)");
    36. e.printStackTrace();
    37. return -1;
    38. }
    39. finally
    40. {
    41. stream.close();
    42. }
    43. }
    44. else
    45. return -1;
    46. }
    47. catch(IOException ioe)
    48. {
    49. /* was auch immer */
    50. this.plugin.info2log("debug2");
    51. return -1;
    52. }
    53. }

    Note that this is old code. You could also copy direclty from a StreamReader to a StreamWriter, but I don't know in which plugin I used what and I don't want to search in all my plugins codes right now... ^^
     
  15. Offline

    SnowGears

    It doesn't like this line
    BufferedWriter writer = new BufferedWriter(output); // The constructor BufferedWriter(FileOutputStream) is undefined
    Or this line
    while (line = reader.nextLine() != null) //The method nextLine() is undefined for the type BufferedReader
     
  16. Offline

    r0306

    Tooner101
    Sorry. I just coded that so it had a few errors. Here's the fixed code:
    Code:
        public void generateTextFile()
        {
          File file = new File("", "text.txt");
         
          //create new file method
          InputStream input = getClass().getResourceAsStream("directory of file inside jar file relative to this class");
          BufferedReader reader = new BufferedReader(new InputStreamReader(input));
       
          FileWriter output = new FileWriter(file.getPath());
          BufferedWriter writer = new BufferedWriter(output);
       
          String line = "";
       
          while ((line = reader.readLine()) != null)
          {
            writer.write(line);
          }
       
          writer.close();
          reader.close();
        }
     
    Tooner101 likes this.
  17. Offline

    SnowGears

    Its all good. Im having trouble locating the directory of my .txt file though. (sorry being a bit nooby) If I have a text file called readme.txt in the same layer as my config.yml, would the "directory of file inside jar file relative to this class" be "/readme.txt" ?

    EDIT: By the way, I'm having all sorts of errors with that new code :/
     
  18. Try this:
    Code:java
    1. InputStream in = this.getClass().getResourceAsStream("/path/to/the/file/in/the.jar");
    2. OutputStream out = new FileOutputStream(yourFile);
    3. byte[] buffer = new byte[1024];
    4. int have;
    5. while((have = stream.read(buffer)) > -1)
    6. out.write(buffer, 0, have);

    Sorry if somebody wrote faster, but I needed some time to figure this out for myself... ^^
     
    Tooner101 likes this.
  19. Offline

    r0306

    Tooner101
    Call up this method after creating the File object and replace yourtxtfilename.txt with the name you defined in the File parameter. The line shown below here: File file = new File(getDataFolder(), "text.txt");

    Code:
    public void createYourTxtFile(){
     
    File file = new File(getDataFolder(), "text.txt");
     
    if (!(getDataFolder().exists())) {
     
    getDataFolder().mkdir();
    }
    if (!file.isFile()) file.createNewFile();
     
    }
    
    For the errors, just follow the compiler and add try catch statements and everything should work.
     
  20. r0306 And here's your code glued with my code:
    Code:java
    1. public void copyFileIfNotPresent()
    2. {
    3. File file = new File(getDataFolder(), "text.txt");
    4. if (!file.exists())
    5. {
    6. getDataFolder.mkdirs();
    7. file.createNewFile();
    8. }
    9. InputStream in = this.getClass().getResourceAsStream("/path/to/the/file/in/the.jar");
    10. OutputStream out = new FileOutputStream(file);
    11. byte[] buffer = new byte[1024];
    12. int have;
    13. while((have = stream.read(buffer)) > -1)
    14. out.write(buffer, 0, have);
    15. }

    ^^
     
    Tooner101 and r0306 like this.
  21. Offline

    SnowGears

    while((have = stream.read(buffer)) > -1) //STREAM CANNOT BE RESOLVED

    What do I do to fix that?
     
  22. Sorry, copy&paste error. Change stream to in ;)
     
  23. Offline

    SnowGears

    oh okay haha. thanks. ill let you know if it works...

    EDIT: It works perfectly! Thank you so much for your help and r0306's help. BTW r0306 ,we still gotta nail down that hitbox issue while riding mob :p.
     
  24. Offline

    r0306

    Tooner101
    Oh right. I'm gonna test the code out.
     
    Tooner101 likes this.
Thread Status:
Not open for further replies.

Share This Page