आप FilterInputStream उपclassing करने का प्रयास कर सकते हैं।
डॉक्स से
:
एक FilterInputStream कुछ अन्य इनपुट धारा है, जो यह डेटा, संभवतः रास्ता साथ डेटा बदलने या अतिरिक्त कार्यक्षमता प्रदान करने के अपने बुनियादी स्रोत के रूप में उपयोग करता है शामिल हैं। क्लास FilterInputStream स्वयं इनपुटस्ट्रीम के सभी तरीकों को उन संस्करणों के साथ ओवरराइड करता है जो निहित इनपुट स्ट्रीम के सभी अनुरोध पास करते हैं। FilterInputStream के उप-वर्ग इन विधियों में से कुछ को ओवरराइड कर सकते हैं और अतिरिक्त विधियां और फ़ील्ड भी प्रदान कर सकते हैं।
यहाँ यह पर एक प्रारंभिक वार है। इसे हल करने का सबसे अच्छा तरीका नहीं है। आप शायद कुछ और तरीकों को ओवरराइड करना चाहते हैं, और शायद इसके बजाय एक पाठक के साथ जाना चाहते हैं। (या शायद यह भी एक स्कैनर का उपयोग करें और लाइन द्वारा फ़ाइल लाइन की प्रक्रिया।)
import java.io.*;
import java.util.*;
public class Test {
public static void main(String args[]) throws IOException {
String str = "Hello world, this is the value one ${bar} and this " +
"is the value two ${foo}";
// The "original" input stream could just as well be a FileInputStream.
InputStream someInputStream = new StringBufferInputStream(str);
InputStream modified = new SubstitutionStream(someInputStream);
int c;
while ((c = modified.read()) != -1)
System.out.print((char) c);
modified.close();
}
}
class SubstitutionStream extends FilterInputStream {
Map<String, String> valuation = new HashMap<String, String>() {{
put("foo", "123");
put("bar", "789");
}};
public SubstitutionStream(InputStream src) {
super(src);
}
LinkedList<Character> buf = new LinkedList<Character>();
public int read() throws IOException {
if (!buf.isEmpty())
return buf.remove();
int c = super.read();
if (c != '$')
return c;
int c2 = super.read();
if (c2 == '{') {
StringBuffer varId = new StringBuffer();
while ((c2 = super.read()) != '}')
varId.append((char) c2);
for (char vc : valuation.get(varId.toString()).toCharArray())
buf.add(vc);
return buf.remove();
} else {
buf.add((char) c2);
return c;
}
}
}
आउटपुट:
Hello world, this is the value one 789 and this is the value two 123
+1 - इस उत्तर ने मुझे तुरंत अपना टेक्स्ट हटा दिया। –
हाय @ बालससी क्या आपका उत्तर एक्सेल फाइलों पर भी लागू है? मैं इनपुटस्ट्रीम संपादित करना चाहता हूं और इसे उपयोगकर्ता को भेजना चाहता हूं। यहां मेरी पोस्ट है: http://stackoverflow.com/questions/34893954/grails-edit-form-multipart-file-from-client-and-send-it-back-to-the-clien?noredirect=1#comment57594193_34893954 धन्यवाद आप। – user3714598