Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The most efficient way would be to write a FilterReader that replaces the starting and ending strings while you are reading the file. I don't see the need for a regular expression to replace static data. The following code could do the trick:</p> <pre><code>public class ReplaceFilterReader extends Reader { private String match; private String replace; private BufferedReader in; private boolean matched; private int currentPos; private boolean end = false; public ReplaceFilterReader(Reader in, String match, String replace) { this.in = new BufferedReader(in); this.matched = false; this.currentPos = 0; this.match = match; this.replace = replace; } @Override public int read(CharBuffer target) throws IOException { int len = target.remaining(); char[] cbuf = new char[len]; int n = read(cbuf, 0, len); if (n &gt; 0) { target.put(cbuf, 0, n); } return n; } @Override public int read(char[] cbuf) throws IOException { return this.read(cbuf, 0, cbuf.length); } @Override public int read() throws IOException { char cb[] = new char[1]; if (this.read(cb, 0, 1) == -1) { return -1; } else { return cb[0]; } } private int readNext() throws IOException { int result = 0; if (!matched) { this.in.mark(match.length()); char cb[] = new char[match.length()]; int n = this.in.read(cb); if (n&gt;0) { String s = new String(cb); if (s.equals(match)) { this.matched = true; if (replace.length()&gt;0) { result = replace.charAt(currentPos); currentPos+=1; if (currentPos == replace.length()) { this.matched = false; this.currentPos = 0; } } else { this.matched = false; this.currentPos = 0; result = 0; } } else { this.in.reset(); result = this.in.read(); } } else { result = n; } } else { result = replace.charAt(currentPos); currentPos+=1; if (currentPos == replace.length()) { this.matched = false; this.currentPos = 0; } } return result; } @Override public int read(char[] cbuf, int off, int len) throws IOException { if (end) { return -1; } int n = 0; int read = 0; for (int i=0; i&lt;len &amp;&amp; n!=-1; i++) { n = this.readNext(); if (n!=-1 &amp;&amp; n!=0) { read += 1; cbuf[off+i] = (char) n; } else if (n==0) { i = i-1; } } if (n == -1) { end = true; } return read; } @Override public void close() throws IOException { this.in.close(); } } </code></pre> <p>And you would proceed like this:</p> <pre><code>Reader yourReader = // open file ... Reader replaceStart = new ReplaceFilterReader(yourReader,"@@GOTO:\"",""); Reader replaceEnd = new ReplaceFilterReader(replaceStart,"\"@@",""); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload