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!!

Recommended Answers

All 6 Replies

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)

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.

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.

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.

use scanner it might help you.

commented: Bad advice, years too late. -3

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??

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.