Results 1 to 6 of 6
  1. #1
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    6

    Default how to create folder and copy documents from JAR file into folder with code?

    Where can I learn or how can I, being the most efficient way known to do so, create a folder outside my JAR file with the java source code, this then will copy YAML documents from my JAR file to that folder, then I need to read the YAML documents some way. I'm making an addon for a game, I am using an API that allows you to make a config.yml easily, and add and read entries from it, but I've read that I need to make my own methods to be able to create additional YAML documents. Can someone link me to a website with this specific topic, about creating YAML documents and reading them, or can someone help me out with some code and explaining?

    Note: I don't exactly know what to type in google to perhaps find such a page, but I did try to find a tutorial page about this or something and couldn't.

    Edit: I've partially figured out how to create a folder, but I have a problem, how can I RETURN one directory to make the folder, I don't want to make the folder in the JAR file I want to make it just outside the Jar file in the same folder that the Jar file is at.

    Regards
    Aqualight
    Last edited by augustas656; 09-08-2014 at 08:13 PM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: how to create folder and copy documents from JAR file into folder with code?

    Quote Originally Posted by augustas656 View Post
    I don't want to make the folder in the JAR file I want to make it just outside the Jar file in the same folder that the Jar file is at.
    You can't make a folder in a jar file. a jar or zip utility may display folder icons, but a jar or zip file just contains entries.

    Java Code:
    new File(YourClassName.class.getProtectionDomain().getCodeSource().getLocation().getPath());
    // OR
    new File(YourClassName.class.getProtectionDomain().getCodeSource().getLocation().toURI());
    db
    Last edited by DarrylBurke; 09-08-2014 at 09:54 PM.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    6

    Default Re: how to create folder and copy documents from JAR file into folder with code?

    Thanks, just earlier I've found a short way, not sure if it will work if put to practise but it works with Eclipse projects, I did:
    Java Code:
    File folder = new File("../NewFolder");
    folder.mkdir();
    For me it just creates a folder one directory back. I'm going to put it to practise, but if this is wrong and won't work if you can tell me earlier then please do, thanks too.

    Regards
    Augustas656

  4. #4
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    6

    Default Re: how to create folder and copy documents from JAR file into folder with code?

    Nevermind, I just did:
    Java Code:
    File folder = new File("Foldername");
    folder.mkdir();
    This just done what I wanted, created a folder outside the Jar, I've exported as Runnable Jar, but I've read there's no difference from Runnable and Non-Runnable except that they have a defined main class to be executed when run, and non-runnable Jars are just Libraries. Now for making YAML Documents, reading and writing them...

    Regards
    Augustas656

  5. #5
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    6

    Default Re: how to create folder and copy documents from JAR file into folder with code?

    I made a permissions.yml file in my project outside of the source code folder but it'll be in the JAR. This is the default permission.yml file, how can I copy this file from the JAR to the folder that the JAR file is in the same way as I made the directory with the code above? Basically I'm going to make it so when this game addon is enabled, it checks for each and every YAML file's existance and also the directory folder that these files are in, if the folder is missing one will be created, and so for all the YAML files. I don't mind about the YAML files contents inside because if they are messed up by the configurer of the addon then they can change or delete these files to get the new ones or make backups or whatever they want to do.

    Can anyone provide me with code and explanation to how to achieve any of these, however, the least I ask is if you remember or know of any useful and clear websites with tutorials about this, perhaps not just about these questions but a whole unit, a section on it's own that would help me learn to work with files, copying them, reading them and about directories etc, I'd be very thankful.

    Regards
    Augustas656

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: how to create folder and copy documents from JAR file into folder with code?

    Get an InputStream from your resource in the .jar file (read the API docs for the Class class) and create a FileOutputStream for you file; keep reading from the InputStream and writing to the (File)OutputStream; don't forget to close both streams afterwards.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. code to create a folder in java
    By radhika123 in forum New To Java
    Replies: 7
    Last Post: 07-21-2011, 11:21 AM
  2. Replies: 1
    Last Post: 04-01-2011, 08:48 PM
  3. Copy single file to another folder
    By ashin in forum SWT / JFace
    Replies: 3
    Last Post: 02-17-2011, 02:01 AM
  4. JAVA source code to create the PRN folder
    By 123456 in forum New To Java
    Replies: 2
    Last Post: 06-14-2010, 12:54 PM
  5. Copy a file to a folder.
    By leric in forum New To Java
    Replies: 7
    Last Post: 07-29-2009, 05:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •