Read a file after a specific word using java
I have to read a file and write into another but after a specific word occurs somethin开发者_如何学Gog like hi. How can i do any help please.
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.
精彩评论