Of course, if you find the Scanner and Formatter classes, you can point them at text files, and they are much easier to use. In the tutorials, look for the contents and use ctrl-f "scanning" or "formatting". It is there (actually in the section I told you about).

1 can be done using java.util.Properties in combination with its load methods (use a Reader, not an InputStream; after all, it's text you want to read, not bytes).

Do you want to replace all occurrences of all keys of the mapping with their values? If so, you can use keySet() to access the keys of the Properties object (which is still a Map so it has the keySet() method), or stringPropertyNames(). I'd prefer the former as that doesn't create a new Set.

This simple approach has one possible flaw: what if the value for some key contains some other key? For instance, what if my HOSTNAME=VOIP_Server? The replacing of IP will then turn the HOSTNAME not into VOIP_Server but into VO10.11.12.13_Server. This in turn can be fixed by doing the find-and-replace in one single loop, using a regular expression. Just group them all together using |:
Now use Pattern and Matcher to find all the occurrences of the keys, replacing each occurrence with the right value.

Hi & thanks again for the advice and direction.
Below I have a working example whereby:
Load the properties file.
read in all keys from the properties file.
read in a source file
substitute keys with element values
write to a dest file

Problem I am having is that only the first key is being subsituted with the appropriate element. Remaining Keys are not.

Well, it's pretty obvious. For the first key you read the entire file. The BufferedReader is then at its end. For the next key, it isn't reset, so it's still at its end, and the readLine() method immediately returns null. I suggest you store the contents of the file into a String, or perhaps a List<String>. If you use a single String I suggest you use a StringBuilder for creating it. It's more efficient than using += on Strings multiple times. Likewise for your String result. I'd change that into a StringBuilder as well. Modifying your code to use a StringBuilder:

Adrian Burke
Greenhorn

Joined: Apr 12, 2011
Posts: 11

posted Jun 08, 2011 11:02:55

0

Thank you for the suggestion for using StringBuilder.
Even with storing the contents of the file in a String - I don't see how I get round the problem of "For the next key, it isn't reset, so it's still at its end, and the readLine() method immediately returns null"

Adrian.

Adrian Burke
Greenhorn

Joined: Apr 12, 2011
Posts: 11

posted Jun 08, 2011 15:23:41

0

Hi,
Have updated so that the contents of the file is stored in a String.