Access data from file

DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Access data from file

  1. #1
    Join Date
    Nov 2002

    Access data from file

    How can i access a particular data from a file

    (filename: test.txt) contains data as follows

    NAME John Smith
    AGE 25
    HEIGHT 170cm
    WEIGHT 80kg

    let say i want to acces NAME from that file that has data "John Smith" , how can i do it
    anyone can help me

  2. #2
    Join Date
    Nov 2002
    IO is somewhat hard to teach just to start out with. So here's a tutorial on it:

    To give you a few hints, use a BufferedReader with a
    File reader like so:
    BufferedReader br = new BufferedReader(new FileReader("test.txt"));
    String st = br.readLine();
    while (st.equals(null) == false) {
        if st.startsWith("NAME") {
           return st;
    Or something of the like

  3. #3
    Join Date
    Nov 2002

    Thanks it works out!

    Thanks a lot its work out,
    there is another thing i hope u don't mind answering my question, can u show me how to write the data that i've read to another file
    I did as below but when i open the output file nothing was there(blank)

    BufferedReader br = new BufferedReader(new FileReader("test.txt");

    BufferedWriter bw = new BufferedWriter(new FileWriter("out.txt");

    if (st.equals(null) == false) {
    if( st.startsWith("NAME") {

  4. #4
    Join Date
    Nov 2002
    or you could TRY the Properties class of the java.util package. it's simple, and suited for this stuff. no loops needed. you just get the key ( e.g. NAME ) then it returns a string that that key holds ( e.g. John Smith ). so the file would look something like -- NAME=John Smith. you could then add more attributes for that particular person. like birthday, nickname, etc.... but it still needs IO. just read the source code of this class. it's in that zipped file called src or source ( i am not so sure, but it's somewhere there ). it should come in your JDK. it's been there since i started using jdk 1.2.

  5. #5
    Join Date
    Nov 2002
    The only problem that I see with that is no String st = br.readLine(); Don't forget that you have to read the file...not just instantiate it.

    Plus, I never use BufferedWriters...I usually use PrinterWriter. It allows for println(...) and print(...) so it seems much easier to me, though both should work.

    Also, another tip while writing is that the readLine() gets rid of the "\n" you have to add it back in.

    You might also do a System.out.println to make sure that your while loop is working like intended. If it doesn't, you know there's a problem with reading, like the readLine().

    If you're really wanting something fun, you might try looking up JDBC in the's a little hard to get into, but once you do, it's a breeze. This would allow you to output to a database file rather than a txt file.

    If the only problem is SQL, try
    One of the best basic SQL tutorials I've found...hope this didn't confuse you too much...if so, let me know. ;-p
    Also know that the database isn't the solution for every program most desktop users will *probably be using an outdated or non-updated version of they might not even have the ODBC's just an easy way to work if it's for you, or possibly a corporate customer.

    Hope this helped...G'Luck

  6. #6
    Join Date
    Nov 2002


    basing on the thread starters problem, i think suggesting that the latter learn JDBC while he can't even do simple IO stuff, is like a jump to the moon. but JDBC, as you say, is really fun. simple question, simple solution.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center