Uploaded image for project: 'Commons IO'
  1. Commons IO
  2. IO-199

Replace strings in file

    XMLWordPrintableJSON

    Details

    • Type: New Feature
    • Status: Open
    • Priority: Minor
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: 3.x
    • Component/s: Utilities
    • Labels:
      None

      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:

      The packet must arrive between @dateFrom and @dateTo. 
      

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

      Perhaps something like this:

      	
      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();
      	}
      

      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

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                a_stroell Alexander Stroell
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated: