Results 1 to 6 of 6
  1. #1
    SilentCoder is offline Member
    Join Date
    Jun 2011
    Posts
    8
    Rep Power
    0

    Default Writing and Reading from a file?

    Hi, i wanted to know how to read and write to a text file for example the text file below.

    I want to read particular's person's information and write/update new information!

    Java Code:
    [Silentcoder]
    TotalGames=100
    TotalHits=100
    TotalMiss=100
    Thank You.

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

  3. #3
    SilentCoder is offline Member
    Join Date
    Jun 2011
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    i found this code but it reads everything from the file how can i make it read about particular person? Thank You

    Java Code:
    import java.io.*;
    class FileRead
    {
        public static void main (String args[])
        {
            try
            {
                // Open the file that is the first
                // command line parameter
                FileInputStream fstream = new FileInputStream ("test.txt");
                // Get the object of DataInputStream
                DataInputStream in = new DataInputStream (fstream);
                BufferedReader br = new BufferedReader (new InputStreamReader (in));
                String strLine;
                //Read File Line By Line
                while ((strLine = br.readLine ()) != null)
                {
                    // Print the content on the console
                    System.out.println (strLine);
                }
                //Close the input stream
                in.close ();
            }
            catch (Exception e)
            { //Catch exception if any
                System.err.println ("Error: " + e.getMessage ());
            }
        }
    }

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    If you are reading the lines you can store them and make changes to them. You may want to make a class to represent people, then read a line, create a person object and store the person somewhere. Finally you can make changes to the stored person or write them back to the file.

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

    Default

    If you have multiple blobs of data then read the file one blob at a time, create an object to represent that data and then store your object in a collection. Now you have all your data in memory and you can access and modify it as much as you like. Then when you have finished write it back to the file.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default

    how can i make it read about particular person
    If the info for each person was written on cards and the cards were in a pile and you could only pick them up one at at time, how would you do it?
    Pick up a card
    look at the name on the card
    is it the one your want?
    Yes - ok use it to ...
    No - skip it

Similar Threads

  1. Reading from .xls file and writing to .csv
    By Yatta in forum New To Java
    Replies: 1
    Last Post: 04-09-2011, 05:44 PM
  2. File reading / writing
    By MattBSibley in forum New To Java
    Replies: 5
    Last Post: 04-19-2010, 06:20 AM
  3. Reading and writing to a file
    By jigglywiggly in forum New To Java
    Replies: 13
    Last Post: 03-09-2009, 11:44 AM
  4. Reading/Writing to file
    By Doctor Cactus in forum New To Java
    Replies: 2
    Last Post: 10-28-2008, 03:05 PM
  5. Help with File reading and writing
    By baltimore in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 07:47 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
  •