I have to read a file and write into another but after a specific word occurs something like hi. How can i do any help please.
+2
A:
Well, something along the lines of:
- Open the input file for reading
- Open the output file for writing
- Keep a
boolean
variable to say whether you're meant to be writing or not - Read a line from the input file (or whatever your unit is)
- If the line is null, or the file is otherwise finished, go to step 8
- If you're meant to be writing, write it to the output file; otherwise, check whether the line contains the word you're interested in
- Go back to step 4 (this will be via a
while
loop, probably) - Close the output file (in a
finally
block) - Close the input file (in a
finally
block)
Now, that's a rough outline, and it skips details such as how you find the word in a line (what if it's in another word?) and whether you have to write out "half a line" if the word occurs in the middle of the line. However, it should be enough to get you started. If you have problems with any of the steps, please give more details so we can help you further.
Jon Skeet
2010-07-01 06:25:43
I tried this. What i need is I have to write the whole file after a specific word occurs. If once the word occurs, from that position i have to write rest all data into another file.
Samurai
2010-07-01 06:39:39
@Samurai: Er, yes. Hence the boolean variable to say whether you're meant to be writing. You would set that to true once you've spotted the word.
Jon Skeet
2010-07-01 06:47:16
Thanks for your reply. I got solution from your reply.
Samurai
2010-07-01 06:49:42