• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Read files and replace Strings

 
Adrian Burke
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you've basically spelled out what you need to do. Where are you stuck?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49813
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is where I am at. I am only catering for 1 value to be replaced.


 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Adrian Burke
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Have updated so that the contents of the file is stored in a String.



Thanks
 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic