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 4 of 4

Thread: Making computer independent file path

  1. #1
    Junior Member
    Join Date
    Aug 2010
    Posts
    23
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Making computer independent file path

    How can I add some files into my project that will have file path independent from computer on which program is executed. I mean, I am using file "Names.txt" located on desktop and in order to read/write in it I have to use file-path something like "C:\\Users\\XYZ\\Desktop\\Names.txt" where XYZ is user-name dependent.
    (I have created a new package called Files and saved "Names.txt" in Files Is there any "short" file path now something like "src\\Files\\Names.txt or so?)


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Making computer independent file path

    You could use a relative path from the 'current' directory. Have you tried: Files/Names.txt?
    Write a short test program to use various paths to see where output is written to.

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

    Javabeginner (September 2nd, 2010)

  4. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Making computer independent file path

    You can read/write a file relative to your project using the 'getClass().getResource()' function (see Class ). For example, if your class is named File/MyClass, then
    MyClass cl = new MyClass();
    URL file = cl.getClass().getResource();
    File myFile = new File(file.toURI());
    the URL gives the path to the file. This should allow you to read/write to a file in the File package.

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

    Javabeginner (September 2nd, 2010)

  6. #4
    Junior Member
    Join Date
    Aug 2010
    Posts
    23
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Making computer independent file path

    It helped Norm I created one output file with name "Dndndndn.txt" and found where it was saved.

Similar Threads

  1. Database in a standalone computer
    By Abednego Setyawan in forum JDBC & Databases
    Replies: 1
    Last Post: April 7th, 2010, 09:24 PM
  2. run java class file from independent of OS
    By starsoheil in forum Java Theory & Questions
    Replies: 6
    Last Post: March 16th, 2010, 08:05 AM
  3. How to execute a Java program independent of Netbeans.
    By ShaunB in forum Java Theory & Questions
    Replies: 4
    Last Post: January 19th, 2010, 06:23 AM
  4. how to get full path name from realtive path
    By priyanka3006 in forum File I/O & Other I/O Streams
    Replies: 8
    Last Post: August 10th, 2009, 04:28 AM
  5. Code wise working of Computer Algebra Systems
    By helloworld922 in forum Java Theory & Questions
    Replies: 1
    Last Post: July 7th, 2009, 07:45 AM