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!
Thank You.Code:[Silentcoder]
TotalGames=100
TotalHits=100
TotalMiss=100
Printable View
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!
Thank You.Code:[Silentcoder]
TotalGames=100
TotalHits=100
TotalMiss=100
This tutorial might help you: Character Streams (The Java™ Tutorials > Essential Classes > Basic I/O)
i found this code but it reads everything from the file how can i make it read about particular person? Thank You
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 ());
}
}
}
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.
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.
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?Quote:
how can i make it read about particular person
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