Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File Handling in Java

Status
Not open for further replies.

6681510

Programmer
Aug 29, 2007
16
FI
Hi , I would like to know how to get data from textfile to hashes .

now i have a textfile which looks like this
Code:
name=>Jhon
id=>102100
address=>linnoituksentie 10

now i want fetch the data from this file in a hash but keys are going to name,id,address and values are going to be jhon,102100,linnoituksentie 10 so that mean i need to sperate the keys and values from the textfile then put the keys and values in hash , write now i can print the whole file on screen . so the code i have now is

Code:
try { FileReader file = new FileReader ("c://INSTLOG.TXT");
	BufferedReader buff = new BufferedReader (file);
	boolean eof = false;
	int a = 0;
	while (!eof) {
	String Inp = buff.readLine();
	a++;
	if (Inp == null)
	eof = true;
	else
	System.out.println(Inp);
	}
	buff.close();
	}
	catch (IOException e) {
	System.out.println ("Error ...." + e.toString());
	}
	}

 
Your textfile data has to be delimited with a character like space or comma.

then in your code, use the buffered reader to read each line.
,
then use a stringTokenizer in java.util.package to break up each line using the delimited, and assign which ever token you want to key and he other as value.
Code:
 String Inp = buff.readLine();
 StringTokenizer tok=new StringTokenizer(Inp,",");
 String key=(String)tok.nextToken();
 String value=(String)tok.nextToken();
 ....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top