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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File Parsing - PLEASE HELP

Status
Not open for further replies.

fatcodeguy

Programmer
Feb 25, 2002
281
CA
Hi, I have a few problems. The first is reading lines from a file.

myfile.txt

this1;is1;my;1file1;ok1
this2;is2;my2;file2;ok2
this3;is3;my3;file3;ok3
this3;is3;my3;file3;ok3

---- code piece ---
public void parseFile(){

String str1,str2,str3,str4,str5,line;
BufferedReader dbms;

//file
try{dbms = new BufferedReader(new FileReader("dbms.cfg"));}
catch(FileNotFoundException ee){}

try{line=dbms.readLine();}catch(IOException ee){}

StringTokenizer st = new StringTokenizer(line,";");

while(st.hasMoreTokens()){
str1=st.nextToken();
str2=st.nextToken();
str3=st.nextToken();
str4=st.nextToken();
str5=st.nextToken();
}
System.out.print(str1+" "+str2+" "+str3+" "+str4+" "+str5);

}//end method parseFile

--end of piece--
this will output --> this1 is1 my 1file1 ok1

I don't know how to read the other lines (and detect end of file). Additionally, I would like to know how to delete a specific line (say, the second line) without having to rewrite the whole file. Finally, I would like to append to the file.

Any help would be great! Thanks!!
 
Read the lines in a loop:

String line = null;
while ((line = dbms.readLine()) != null)
{
StringTokenizer st = new StringTokenizer(line,";");

while(st.hasMoreTokens()){
str1=st.nextToken();
str2=st.nextToken();
str3=st.nextToken();
str4=st.nextToken();
str5=st.nextToken();
}
System.out.print(str1+" "+str2+" "+str3+" "+str4+" "+str5);
}

 
Great! Thanks. How would I "seek" through the file to a specific line/word, and then how would I delete it?
 
If you want to do 'seeking' you need to use a different file access method. The RandomAccessFile class is used for this purpose. The problem is, you can't really 'delete' data from the file. You would have to read in the data, remove the data you want to delete and rewrite the file.

You can read up on the RAF in the Java API.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top