0

I have a file lets call it template.cfg and I want to make changes to it and save the file with a new name preserving the template file.

Template file contents. There is alot more in the file however these are the online lines i'm concerned about.

<property>URL</property>
<property>PASSWORD</property>
<property>SCHEMA</property>
<property>USER</property>

Output file I want to have a new name lets call it databaseInfo.cfg So i'd need to search for the words URL and replace it with a user defined URL same with PASSWORD SCHEMA and USER. I would also like to save it in the same directory as the template.

<property>foo</property>
<property>bar</property>
<property>D2D</property>
<property>D2Duser</property>

I'm thinking in terms like Save As. not sure how I should go about modifying the file without changing the original and yet saving it as a new file.

I dont have any code yet outside of getting the template files full path to open the file because I'm curious what is the best route to take from having the file.

File file = new File(template.cfg);
String fullPath = file.getAbsolutePath();
Not a bug
  • 4,286
  • 2
  • 40
  • 80
Jeremy
  • 935
  • 5
  • 18
  • 33
  • I wonder why the file extension is 'CFG'. Is the input a real, valid, structured XML file? Then you can parse the file as a DOM tree, modify the nodes, and write it back as an XML file. It's a comparatively high effort, but ... (I don't want to mention any quick&dirty solutions involving things like BufferedReader#readLine and String#replace for now... ) – Marco13 Jan 31 '14 at 16:43
  • http://fmpp.sourceforge.net/ (and FreeMarker itself) can do it at a cost of 3 lines in your pom.xml – bobah Jan 31 '14 at 16:47
  • the .cfg is a config file generated for use with hibernate. And sorry the file real file extension is template.cfg.xml I'll update it in the question to remove confusion. – Jeremy Jan 31 '14 at 17:24
  • possible duplicate of [Modify a .txt file in Java](http://stackoverflow.com/questions/822150/modify-a-txt-file-in-java) – La-comadreja Jan 31 '14 at 17:34
  • Assuming the awnser in java is to in fact use a String buffer and iterate over each line and write it to another file then the awnser is in essence the same. I was hoping there was a better memory efficent way of doing it than that. – Jeremy Jan 31 '14 at 19:22

1 Answers1

1

You can simultaneously run a Scanner to read the text from the old file, modify the text using String manipulation operations, and use a PrintWriter to write the output strings to a new file. That way, you would have both the old file and the new file.

Alternatively, if your text replacements are particularly straightforward, you could do away with Java altogether and manipulate it on the UNIX command line with the "sed" command. Hope this helps.

La-comadreja
  • 5,627
  • 11
  • 36
  • 64
  • The difficulty that I referred to in the comment was about the potential problem with this approach IF any of the other lines in the file contain one pattern that is about to be matched. If there is another tag like ``, then this occurrence of 'URL' should not be replaced (and it's hard to say whether this case or other border cases can easily be handled...) – Marco13 Jan 31 '14 at 17:08
  • This is how I was thinking of doing it was just wondering if there was another method other than reading and writing each line out to a new file. – Jeremy Jan 31 '14 at 17:26
  • That is the way to do it in Java. For your application, however, I would probably do away with Java and write a shell script using the sed command: http://www.grymoire.com/unix/sed.html – La-comadreja Jan 31 '14 at 17:30
  • I couldn't really find any other way in Java, e.g. see http://stackoverflow.com/questions/20039980/java-replace-line-in-text-file and http://stackoverflow.com/questions/822150/modify-a-txt-file-in-java – La-comadreja Jan 31 '14 at 17:31