[ 
https://issues.apache.org/jira/browse/IO-199?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Alexander Stroell updated IO-199:
---------------------------------

    Description: 
A method, that can replace strings (keys) in a file would be very useful. 

For example if you have a file with the following text:

{noformat}
The packet must arrive between @dateFrom and @dateTo. 
{noformat}

You can now replace the "keys" with a value of your choice..


Perhaps something like this:

{code}  
public static void replaceStrings(File inputFile, File outputFile, 
List<KeyValuePair<String, String>> replacements) throws IOException{
                BufferedReader in = new BufferedReader(new 
FileReader(inputFile));
                BufferedWriter out = new BufferedWriter(new 
FileWriter(outputFile));
                
                String line;
                
                while ((line = in.readLine()) != null) {
                        for(KeyValuePair<String, String> kvp : replacements){
                                line = line.replace(kvp.getKey(), 
kvp.getValue());
                        }
                        out.write(line);
                        out.newLine();
                }
                out.flush();
                out.close();
                in.close();
        }
{code}


I think it should also be possible, to replace the strings in the inputfile so 
you don't have to create a new file (outputfile)

Regards Alex



  was:
A method, that can replace strings (keys) in a file would be very useful. 

For example if you have a file with the following text:

{noformat}
The packet must arrive between @dateFrom and @dateTo. 
{noformat}

You can now replace the "keys" with a value of your choice..


Perhaps something like this:

{code}  
public static void replaceStrings(File inputFile, File outputFile, 
List<KeyValuePair<String, String>> replacements) throws IOException{
                BufferedReader in = new BufferedReader(new 
FileReader(inputFile));
                BufferedWriter out = new BufferedWriter(new 
FileWriter(outputFile));
                
                String line;
                
                while ((line = in.readLine()) != null) {
                        for(KeyValuePair<String, String> kvp : replacements){
                                line = line.replace(kvp.getKey(), 
kvp.getValue());
                        }
                        out.write(line);
                        out.newLine();
                }
                out.flush();
        }
{code}


I think it should also be possible, to replace the strings in the inputfile so 
you don't have to create a new file (outputfile)

Regards Alex




> Replace strings in file
> -----------------------
>
>                 Key: IO-199
>                 URL: https://issues.apache.org/jira/browse/IO-199
>             Project: Commons IO
>          Issue Type: New Feature
>          Components: Utilities
>            Reporter: Alexander Stroell
>            Priority: Minor
>
> A method, that can replace strings (keys) in a file would be very useful. 
> For example if you have a file with the following text:
> {noformat}
> The packet must arrive between @dateFrom and @dateTo. 
> {noformat}
> You can now replace the "keys" with a value of your choice..
> Perhaps something like this:
> {code}        
> public static void replaceStrings(File inputFile, File outputFile, 
> List<KeyValuePair<String, String>> replacements) throws IOException{
>               BufferedReader in = new BufferedReader(new 
> FileReader(inputFile));
>               BufferedWriter out = new BufferedWriter(new 
> FileWriter(outputFile));
>               
>               String line;
>               
>               while ((line = in.readLine()) != null) {
>                       for(KeyValuePair<String, String> kvp : replacements){
>                               line = line.replace(kvp.getKey(), 
> kvp.getValue());
>                       }
>                       out.write(line);
>                       out.newLine();
>               }
>               out.flush();
>               out.close();
>               in.close();
>       }
> {code}
> I think it should also be possible, to replace the strings in the inputfile 
> so you don't have to create a new file (outputfile)
> Regards Alex

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to