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: Loading a file in a different folder

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Loading a file in a different folder

    Hey guys.

    Say I have two folders in my project:
    src/main/java
    src/main/resources

    And I have code in the src/main/java folder, and this code wants to load a file that is in the src/main/resources folder. How would I go about doing that without using the location like this "C:\Documents And Settings\Workspace..."

    Isn't there some way to use that path instead? Like this "/ProjectName/src/main/resources"?

    So my code looks like this:
    FileInputStream fis = new FileInputStream("what goes here?");

    Thanks


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Loading a file in a different folder

    You can use relative pathing. To go up a directory, use "../" as many directories as you need until there is a common folder, at which point start descending down the path you want.

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

    tiemykim (March 14th, 2010)

  4. #3
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Loading a file in a different folder

    I take it this is a Maven project you are working with?

    If so then everything you put in the resources folder will automagically be copied into your classes folder.You might want to have a look at using a classloader to load the file using getResource.

    MyClass.class.getClassLoader().getResourceAsStream("filename");

    // Json

Similar Threads

  1. Loading in images
    By eXcellion in forum Java Theory & Questions
    Replies: 3
    Last Post: January 17th, 2010, 12:13 PM
  2. Replies: 8
    Last Post: January 6th, 2010, 09:59 AM
  3. Prob in Loading Textures
    By sikriyogesh in forum Java Theory & Questions
    Replies: 0
    Last Post: December 15th, 2009, 05:59 AM
  4. Replies: 3
    Last Post: April 14th, 2009, 08:35 AM
  5. Java program to take new files from one path and sent it do another path?
    By jazz2k8 in forum File I/O & Other I/O Streams
    Replies: 11
    Last Post: July 8th, 2008, 06:47 AM