Results 1 to 5 of 5
  1. #1
    chrisLU5 is offline Member
    Join Date
    Apr 2011
    Posts
    16
    Rep Power
    0

    Default Text file extension?

    Hello all, quick question, can i replace:
    Java Code:
    String question = "D:\\Chris Halfpenny - Java Assignment\\Program Part 3\\Question.txt";
    With:
    Java Code:
    String question = "Question.txt";
    I have tried an it comes up with file not found exception.

    Any ideas :D

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    Where is the file? Is it in the current directory?
    One way to find the current directory is to open a file for output and see where it is written.
    I'm sure there is a faster/easier way but I don't have it available right now.

  3. #3
    chrisLU5 is offline Member
    Join Date
    Apr 2011
    Posts
    16
    Rep Power
    0

    Default

    No the program works fine with the first code i have shown, i just wanted to know if theres anyway to shorten the file extension to just "Question.txt" instead of the longer version, its just if i was to move the program to say another hard drive i would have to go back and change the file extension in the program, oh yeah and the question.txt file is in the root of the program.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    It's often better to use a relative path vs the full path.

    You didn't say if you wrote a test program to find where a file would be if you wrote it without any path.
    Try that and see.

  5. #5
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Quote Originally Posted by chrisLU5 View Post
    ... its just if i was to move the program to say another hard drive i would have to go back and change the file extension in the program
    If the program and file might be moved independently, the thing to do would be to pass in the current location of the file to your program as a parameter when you run it.

    Otherwise, if they're always in the same relative position but the drive letter changes, use a relative path as Norm suggested.

Similar Threads

  1. Image File Extension Regular Expression
    By kagster in forum New To Java
    Replies: 1
    Last Post: 02-15-2011, 06:30 PM
  2. File Extension Filter
    By heartysnowy in forum New To Java
    Replies: 9
    Last Post: 10-09-2010, 02:33 PM
  3. save file based on file extension
    By masa in forum AWT / Swing
    Replies: 4
    Last Post: 05-11-2010, 12:17 PM
  4. how to save Interface (file extension)
    By shaggyoo7 in forum New To Java
    Replies: 9
    Last Post: 01-10-2009, 10:07 AM
  5. Regex for file extension
    By gapper in forum New To Java
    Replies: 1
    Last Post: 01-31-2008, 04:59 PM

Posting Permissions

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