Results 1 to 6 of 6
  1. #1
    Oasis13 is offline Member
    Join Date
    Jan 2008
    Posts
    3
    Rep Power
    0

    Default Accessing Data from a .txt file

    Hi guys,
    I hope you are able to help me :D

    I'm very new to Java, and have so far just spent a lot of hours playing around and using google when i get stuck with something.

    Anyway, in a little program I'm currently doing I'd like to set an int variable to a value that is stored in a .txt file.

    For example, in pseudo code it would work something like this:
    Java Code:
    int a;
    a = the value of the int stored in this text file(hello.txt);
    The main purpose of doing this is so that every time the program is run, int a takes its value from the text file. Whilst running the program I might like to change the value of a, in which case the txt file will need changing so that the changes are reflected upon the next run of the programm. Essentially its like using a GET SQL command with a MySQL database, but far simpler!!! (Hopefully :p)

    Any help with this would be greatly appreciated!

  2. #2
    gibsonrocker800's Avatar
    gibsonrocker800 is offline Senior Member
    Join Date
    Nov 2007
    Location
    New York
    Posts
    143
    Rep Power
    0

    Default

    You can use the FileReader class for this.

    Java Code:
    FileReader reader = new FileReader("hello.txt");
    Scanner console = new Scanner(reader); //Use a scanner to read the info
    int x = console.nextInt();
    See basically, Scanner has a constructor for a FileReader, so we can pass the FileReader object to the scanner, thus giving us the ability to use the Scanner methods on it. In this case, we will use the nextInt() method to obtain the int value in the .txt file.

    If you have any more questions, let me know, i enjoy file reading and writing.

  3. #3
    Oasis13 is offline Member
    Join Date
    Jan 2008
    Posts
    3
    Rep Power
    0

    Default

    Thanks, that worked like a charm!!!

    Just one question though - how can i edit the contents of the text file from within the programm e.g. change the value of the int x.

  4. #4
    gibsonrocker800's Avatar
    gibsonrocker800 is offline Senior Member
    Join Date
    Nov 2007
    Location
    New York
    Posts
    143
    Rep Power
    0

    Default

    Well, with this, we will need to use FileWriter. We can overwrite the original file with the new text we want.

    Java Code:
    FileWriter out = new FileWriter("hello.txt");
    out.write(//new value of x);
    out.close();
    By the way, both FileWriter and Reader constructors throw IOException, so you have to handle it. (try, catch)

  5. #5
    Oasis13 is offline Member
    Join Date
    Jan 2008
    Posts
    3
    Rep Power
    0

    Default

    Thanks again, I've got it all working now :D

  6. #6
    gibsonrocker800's Avatar
    gibsonrocker800 is offline Senior Member
    Join Date
    Nov 2007
    Location
    New York
    Posts
    143
    Rep Power
    0

Similar Threads

  1. Axis Client accessing data on .Net webservice
    By cfacile666 in forum Web Frameworks
    Replies: 3
    Last Post: 09-02-2008, 11:35 AM
  2. Accessing a file randomly
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-10-2007, 08:15 PM
  3. Accessing client file system
    By revathi17 in forum New To Java
    Replies: 1
    Last Post: 10-17-2007, 10:17 AM
  4. Packaging and accessing data files
    By todd in forum Advanced Java
    Replies: 1
    Last Post: 08-01-2007, 12:27 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
  •