Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Pointing to file inside project

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Location
    Birmingham, United Kingdom
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Pointing to file inside project

    [EDIT: hierarchy of project have the spaces removed for some strange reason, hopefully the way I've named them makes it clear]

    A nice general question which as actually pretty difficult to solve using Google. I've tried searching for multiple things, but all examples of what I'm looking for don't include this case, here's my problem:

    I have a 'resources' text file that currently sits inside its own package, my project hierarchy is as follows:

    Project
    cardPackage
    aCardClass
    anotherCardClass
    mainPackage
    mainClass
    screenDrawingClass
    resourcePackage
    resources.txt

    A method inside one of my card classes accesses the resources.txt and reads each line. It iterates through the lines until it finds the keyword that marks the start of that certain card's data, and then reads the following lines storing that data into the instance variables of the card. As it stands at the moment, I read the file with a FileReader, and include the entire path to reach the text file (which is inside the workspace/project/package/ location at the moment - using Eclipse IDE). This however needs to be changed eventually, as I'd (eventually) like to make this game playable on more than my own system, and therefore need to use an install path.

    Due to this, I suppose I need an OS-path independent way of accessing resources.txt. While testing (and before settling with using the long path method I'm currently using) I used the getResource function, and accessed it through the main class using:

    File file = new File(Main.class.getResource("resources.txt");

    For this, I put the text file in the same package as the main class. The only problem with this is that there will very likely be much more resource files being used in the future, and it seems coherent to separate them from the rest of the code. If there is a similar method to getResource that can find the file through the package it is in, rather than looking in the same directory as a Class, that would be brilliant, but unfortunately I don't know of one.

    Any ideas you guys have would be massively appreciated! What would you do in my situation?

    Thanks for taking the time to read

    M3ssy


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,099
    Thanks
    169
    Thanked 779 Times in 725 Posts
    Blog Entries
    5

    Default Re: Pointing to file inside project

    You can use typical unix style paths to get the resource from the package. For example,
    Main.class.getResource("/resources.txt") ;///gets the resource.txt in the package root directory
    Main.class.getResource("/images/resources.txt");//gets the resource.txt in the package/directory images

    Alternatively, just calling something like
    File file = new File("temp");
    String jarDirectory = file.getParentFile().getAbsolutePath();
    Will allow you to get the path of the jar itself, so you can read/write files relative to your application outside the jar itself.

  3. The Following User Says Thank You to copeg For This Useful Post:

    M3ss1ah (March 11th, 2011)

  4. #3
    Junior Member
    Join Date
    Feb 2011
    Location
    Birmingham, United Kingdom
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Pointing to file inside project

    Thanks, that's helped a lot!

    M3ssy

Similar Threads

  1. What can be legally inside FOR statement?
    By ice in forum Loops & Control Statements
    Replies: 7
    Last Post: January 13th, 2011, 11:31 AM
  2. how to read file name inside .gz file....
    By kathir0301 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: December 6th, 2010, 09:06 AM
  3. [SOLVED] Why can't I write to file inside a doGet() method?
    By FailMouse in forum Java Servlet
    Replies: 1
    Last Post: July 7th, 2010, 01:15 AM
  4. centering a label inside a rectangle
    By Brain_Child in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 19th, 2009, 08:08 AM
  5. count components inside a JPanel
    By dewboy3d in forum AWT / Java Swing
    Replies: 1
    Last Post: August 2nd, 2009, 03:17 PM