If the text you want to change will always be on the same logical line, as I said in the comment, I would go with a simple read of the line (if applicable), using something like:
public class InputReader { public static void main(String[] args) throws IOException { String string = "This is the test string which needs to be modified"; InputStream inpstr = new ByteArrayInputStream(string.getBytes()); BufferedReader rdr = new BufferedReader(new InputStreamReader(inpstr)); String buf = null; while ((buf = rdr.readLine()) != null) {
However, I always liked to use inheritance, so I would define it somewhere:
class MyReader extends BufferedReader { public MyReader(Reader in) { super(in); } @Override public String readLine() throws IOException { String lBuf = super.readLine();
And use MyReader instead of the standard BufferedReader, keeping the substitution hidden inside the readLine method.
Pros: substitution logic is in the specified Reader, the code is pretty standard. Cons: it hides the logic of substitution to the caller (sometimes itβs also pro, it still depends on the use case)
NTN
source share