aspose file tools*
The moose likes Beginning Java and the fly likes Read files and replace Strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Read files and replace Strings" Watch "Read files and replace Strings" New topic
Author

Read files and replace Strings

Adrian Burke
Greenhorn

Joined: Apr 12, 2011
Posts: 11
Hi
Would like some advice on how to do the following:

1. read a list of String values from a file
eg:
HOSTNAME=myDevelopmentServer
IP=10.11.12.13

2. Read in a second file(file2.txt) and replace String
eg:
Name of server is HOSTNAME.

3. Write the result to a file3.txt.
Eg:
Name of sever is myDevelopmentServer.


fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11477
    
  16

you've basically spelled out what you need to do. Where are you stuck?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
Start with this section of the Java™ tutorials. Remember for text you will want a FileReader and a BufferedReader, and a FileWriter and a BufferedWriter.

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).
Adrian Burke
Greenhorn

Joined: Apr 12, 2011
Posts: 11
This is where I am at. I am only catering for 1 value to be replaced.


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Adrian Burke
Greenhorn

Joined: Apr 12, 2011
Posts: 11
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.



Any further suggestions are most welcome.

Adrian.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

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
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
Hi,
Have updated so that the contents of the file is stored in a String.



Thanks
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

That's exactly what I meant with storing the contents. Because now the entire String is stored in memory you can use it over and over again.
 
Consider Paul's rocket mass heater.
 
subject: Read files and replace Strings