1,105,534 Community Members

How to read the first record in a file in java and delete it

Member Avatar
vartikachandra
Newbie Poster
18 posts since Jan 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

i have to store msgs of string type in a file in java.how do i distinguish between 2 msgs.and how shud i delete the first msg in the file.
for eg:
"My name is abc.I am fbvnfdl.I am a girl." is stored in a file.
and "My name is abc.I am fbvnfdl." is the 1st msg
and "I am a girl." isthe 2nd msg
and i have to delete the 1st msg.This is dynamic string and the length is also unknown.

Please help!
Thanks...its URGENT!!

Member Avatar
quuba
Posting Pro
573 posts since Nov 2008
Reputation Points: 81 [?]
Q&As Helped to Solve: 107 [?]
Skill Endorsements: 0 [?]
 
0
 

Read line from file.
String line="My name is abc.I am fbvnfdl.I am a girl.";
There are three dots.
Write method tokenize, using new StringTokenizer(".")
Dot as delimiter.
Result:
1. "My name is abc"
2. "I am fbvnfdl"
3. "I am a girl"
Write 3. to file with dot at end. (regenerate dot)

Member Avatar
stephen84s
Nearly a Posting Virtuoso
1,328 posts since Jul 2007
Reputation Points: 550 [?]
Q&As Helped to Solve: 160 [?]
Skill Endorsements: 13 [?]
Featured
 
0
 

Do not use StringTokenizer , it is a legacy class, to quote the javadocs :-

StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.

So as recommended use the split() method of the String class instead.

Also as far as deleting content from a file is concerned, you have two options.

In the first, you will need to copy all the content from the first file into a String (or StringBuffer) do all the modifications as necessary to this String, write this new content to a temporary file, delete the original file and then rename the temporary file with the name of the original file.

In the second method you can use the RandomAccessFile class, but I have been told, if you are not careful it can get messy.

Member Avatar
vartikachandra
Newbie Poster
18 posts since Jan 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Do not use StringTokenizer , it is a legacy class, to quote the javadocs :-

So as recommended use the split() method of the String class instead.

Also as far as deleting content from a file is concerned, you have two options.

In the first, you will need to copy all the content from the first file into a String (or StringBuffer) do all the modifications as necessary to this String, write this new content to a temporary file, delete the original file and then rename the temporary file with the name of the original file.

In the second method you can use the RandomAccessFile class, but I have been told, if you are not careful it can get messy.

ThankYou so much!!!I thinki will put a delimiter after evry msg For eg.a "$" and then use the temoprary file concept.

Member Avatar
stephen84s
Nearly a Posting Virtuoso
1,328 posts since Jul 2007
Reputation Points: 550 [?]
Q&As Helped to Solve: 160 [?]
Skill Endorsements: 13 [?]
Featured
 
0
 

ThankYou so much!!!I thinki will put a delimiter after evry msg For eg.a "$" and then use the temoprary file concept.

No need to actually put a "$" after every message, you could simply put every message on a new line in the file and then split on "\n". for ex:-

This is message1
This is message2
.
.
.

and so on.

Note:-
Please drop the Instant Messaging / SMS speak eg (evry, msg), it is not exactly pleasing to the eyes to read it.

Member Avatar
rushikesh jadha
Junior Poster in Training
89 posts since Dec 2011
Reputation Points: -6 [?]
Q&As Helped to Solve: 11 [?]
Skill Endorsements: 0 [?]
 
-1
 

use scanner it might help you.

Member Avatar
stultuske
Posting Expert
5,339 posts since Jan 2007
Reputation Points: 938 [?]
Q&As Helped to Solve: 777 [?]
Skill Endorsements: 35 [?]
Featured
 
1
 

use scanner it might help you.

seriously? trying to solve a thread that was solved (since the "Thank you very much" from the OP) about 3 years ago??

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article