Results 1 to 7 of 7

Thread: Read from file

  1. #1
    talia is offline Member
    Join Date
    Dec 2011
    Location
    London
    Posts
    11
    Rep Power
    0

    Default Read from file

    hi,

    am trying to create a login method, where details are saved in text files 2 different(login details, contact details) I managed to read from the file and display it in objects like text fields, but cannot synchronise the details. I want the person who is logged in contact details displayed, but at the moment I can only make it work so it reads line by line. Therefore no matter who logs in the contact details displayed are the ones on the top of the text file (first 5 lines). How can I make this work?

    thanks for your help, anyone

  2. #2
    joeyvitoro is offline Member
    Join Date
    Oct 2011
    Posts
    90
    Rep Power
    0

    Default Re: Read from file

    You can search the file for the specific username, and start from that line.

  3. #3
    talia is offline Member
    Join Date
    Dec 2011
    Location
    London
    Posts
    11
    Rep Power
    0

    Default Re: Read from file

    thanks,
    but how do I start from that line? that is what I cant get to work, it reads in from the first line... I use BufferedReader should I use Scanner?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,602
    Rep Power
    23

    Default Re: Read from file

    it reads in from the first line
    That is how most text files work. You have start at the first line and read until you get to the line you want.
    In the very unlikely case that all of the lines have exactly the same number of letters in them, you could read a line directly by jumping to the first letter of the line by skipping over the preceding lines which would be the number of the line - 1 times the number of letters in the line. This never happens so don't worry about how to do it.

  5. #5
    joeyvitoro is offline Member
    Join Date
    Oct 2011
    Posts
    90
    Rep Power
    0

    Default Re: Read from file

    You want something like...
    [pseudocode]
    while((tempString = reader.readline) is not null)
    ---if tempstring equals username
    --------skip this line and however many more deal with this user
    ---else
    --------you have the lines you want
    end while

  6. #6
    talia is offline Member
    Join Date
    Dec 2011
    Location
    London
    Posts
    11
    Rep Power
    0

    Default Re: Read from file

    thank you sooo much, worked a treat, cant believe I was soo close to the solution:) can carry on now, am sure I will still have a lot of questions, have an assignment to hand in on monday, and my programming skills are only about 2 months old :)

    thanks again....

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Read from file

    Performing IO is expensive. A better idea would be to read the entire file once into memory. Store all the data in a Collection such as a HashMap. That way retrieving data will be a lot quicker and easier.

Similar Threads

  1. Replies: 1
    Last Post: 08-05-2011, 09:21 PM
  2. Replies: 2
    Last Post: 05-11-2009, 10:07 AM
  3. Replies: 0
    Last Post: 02-11-2009, 09:53 AM
  4. Replies: 5
    Last Post: 02-05-2009, 10:28 AM
  5. How to read a text file from a Java Archive File
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 09:13 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
  •