Results 1 to 7 of 7
  1. #1
    nhmllr's Avatar
    nhmllr is offline Member
    Join Date
    Aug 2011
    Posts
    51
    Rep Power
    0

    Default Reading a user made text file?

    I'm making a little application that is a study helper for history
    What it does is read a text file (written in a specific format) an makes an array of all of the terms and definitions found in the text file, then can quiz you in a few different ways, such as giving the user a term and five definitions and they have to match it correctly.
    I'm pretty far down the line with it, but there's one thing I don't know how to do.

    The program should be pretty robust, in that the set of terms and definitions should be able to change without having to edit the source. When the application is done, I want to be able to simply replace the text file (in the same folder as the app) and have the app read the new text.

    How do you do this? Thanks.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Reading a user made text file?

    Give it a menu or JButton whose Action opens a JFileChooser, allows the user to choose a file, then tries to open and use that file -- all within try/catch blocks of course.

  3. #3
    nhmllr's Avatar
    nhmllr is offline Member
    Join Date
    Aug 2011
    Posts
    51
    Rep Power
    0

    Default Re: Reading a user made text file?

    Is there a way to do it where you could just put the text file in the same folder as the application?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Reading a user made text file?

    Quote Originally Posted by nhmllr View Post
    Is there a way to do it where you could just put the text file in the same folder as the application?
    What if you put your application in a Jar file, which is pretty darn likely. Do you mean to put the text files in the jar file? Is it OK if these files cannot be modified? If so, then sure, put them in the Jar (though I'd put them in their own directory), but be sure to refer to them as resources, not files.

  5. #5
    nhmllr's Avatar
    nhmllr is offline Member
    Join Date
    Aug 2011
    Posts
    51
    Rep Power
    0

    Default Re: Reading a user made text file?

    But if I put it in a Jar, then I CAN'T modify it?
    The point is that I should be able to edit the study sheet whenever I want.

    It's not the worst thing to have to select the text file every time you open the program, though...

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Reading a user made text file?

    Quote Originally Posted by nhmllr View Post
    But if I put it in a Jar, then I CAN'T modify it?
    The point is that I should be able to edit the study sheet whenever I want.
    Jars are not made for modification as the data is stored in zip-file format. Modification isn't impossible but there are usually better ways to solve your problem.

    It's not the worst thing to have to select the text file every time you open the program, though...
    Consider having your program use Properties where you can store the default location of the data files, and then it should be easy to display a file list in a JList. Otherwise if the user doesn't find the file give them the option of using a JFileChooser if need be.

  7. #7
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default Re: Reading a user made text file?

    You can get the current working directory of your executing program using the following snippet:

    Java Code:
    System.getProperty("user.dir")
    Then you can use this information to find where you data file is located relative to your application.
    Last edited by wsaryada; 01-13-2012 at 08:39 AM.

Similar Threads

  1. Arrays made from user input
    By sonofJack3 in forum New To Java
    Replies: 15
    Last Post: 12-01-2011, 06:18 AM
  2. Replies: 8
    Last Post: 07-17-2011, 02:38 PM
  3. Help reading a text file.
    By Kristoba in forum New To Java
    Replies: 5
    Last Post: 03-11-2010, 08:42 AM
  4. Reading in a text file
    By TheRealHoff in forum AWT / Swing
    Replies: 10
    Last Post: 02-07-2010, 11:47 PM
  5. reading text file
    By trofyscarz in forum New To Java
    Replies: 1
    Last Post: 02-05-2010, 02:24 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
  •