Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Java
Parsing a file for a string and then replace it
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Anthony Borla, post: 548884"] The 'StringTokenizer' class may be used to tokenise / parse a 'String', while the 'StreamTonenizer' does the same for stream data [also has more control over the parsing process]. These are both general purpose classes, and you would need to incorporate them into your own parser class. Yes, it is possible: using a 'RandomAccessFile' object allows you can both read and write to the same file. You can avoid creating a new file by: * Using a 'RandomAccessFile' object to write modified data to the same file - care needs to be taken with this approach since it is possible to write over data that has not already been read in [e.g. if replacing a short string with a longer one] Personally, I wouldn't use this approach, but I'd be remiss if I didn't mention it * Read file data into a memory area, make the modifications in memory, then write out the modified data to the same file. You could do something like: - Use 'FileReader' to read all file data into a 'char' array in one step; close this stream when finished - Wrap up the 'char' array inside a 'StringBuffer' object [StringBuffer sb = new StringBuffer(array.length); sb.append(array);], and make any required modifications - Use 'FileWriter' to write the modified 'char' array data to the same file, all in one step Alternatively, you could retain the line-by-line processing approach, and simply append each [modified] line to a 'StringBuffer' instead of writing it out to a stream. Once all the input has been processed, write the 'StringBuffer' contents out via a 'FileWriter' Note that in all cases file timestamp information is updated. if you don't want this, use a 'File' object to store the original timestamp, and reset the updated file to this value. I'm not sure about simpler, though certainly there are several different ways of performing this type of task. Off hand, one thing I'd suggest changing is to make 'newLine' a 'StringBuffer', and do this: newLine .append(line.substring(0, index)) .append(newStr) .append(line.substring(index + str.length()); instead of concatenating 'String' objects, a much slower process. Of course, the performance difference may not matter, but it's always nice to know that your program is as efficient as it could be [without going to extremes :)]. Use these if you require buffering; if not, don't use them. You can always perform timing tests with / without them and determine which offers best performance. I hope this helps. Anthony Borla P.S. I noticed the extension on your data files was '.xml'. If you need to perfom any XML parsing / transformation take a look at the 'javax.xml' packages [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Java
Parsing a file for a string and then replace it
Top