views:

41

answers:

1

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:

  1. Open the input file for reading
  2. Open the output file for writing
  3. Keep a boolean variable to say whether you're meant to be writing or not
  4. Read a line from the input file (or whatever your unit is)
  5. If the line is null, or the file is otherwise finished, go to step 8
  6. 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
  7. Go back to step 4 (this will be via a while loop, probably)
  8. Close the output file (in a finally block)
  9. 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
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
@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
Thanks for your reply. I got solution from your reply.
Samurai