dcsimg
How to delete a line (say the first line) from a file
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Pradeep_Bhargav
Posted On:   Saturday, June 1, 2002 05:11 AM

Hi, I'm developing an application using which I should read the data from a text file and insert the record into a database and the remove the value in the file. Ex. I have a text file named employees.txt in which I have list of employees. Say, employee1 employee2 ... ... I should get the value in the first line i.e employee1 (first time), insert that into a DB then delete value "employee1" in the file. I'm able to get from text file and insert into DB, but I am not knowing how to delete the first line of the file. Regards, pradeepbhargav    More>>

Hi,

			

I'm developing an application using which I should read the
data from a text file and insert the record into a
database and the remove the value in the file.

Ex.

I have a text file named employees.txt in which I have list
of employees.

Say,
employee1
employee2
...
...

I should get the value in the first line i.e employee1 (first time),
insert that into a DB then delete value "employee1" in the file.
I'm able to get from text file and insert into DB, but
I am not knowing how to delete the first line of the file.





Regards,

pradeepbhargav    <<Less

Re: How to delete a line (say the first line) from a file

Posted By:   Anonymous  
Posted On:   Saturday, June 1, 2002 06:04 AM


// line read from the file
String line;

/* now input */
// Name of the input file
String myFileName = ...;
// stream for reading from file
BufferedReader in = new BufferedReader(new FileReader(myFileName));

/* now output-stuff */
BufferedWriter out = new BufferedWriter(new FileWriter(myFileName + ".tmp"));


// readfirst line of the file, we don't care...
line = in.readLine();

// just make sure that the file is NOT empty
if(line == null)
return;

// now copy the rest of the file
while((line = in.readLine()) != null)
{
out.write(line, 0, line.length());
out.newLine();
}

// close streams, perhaps in a finally block
in.close();
out.close();

// now the file without the first line has the extension .tmp

// you have to rename it. make use of File.renameTo() and File.delete();

// of course this code-block is NOT ready for use.
// you've to catch millions of exceptions and so on....



Okay? EyeDentity
About | Sitemap | Contact