Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

replace a string segment from input stream

I am trying to receive a huge text file as an inputstream and want to convert a string segment with another string. I am strictly confused how to do it, it works well if I convert whole inputstream as a string which I don't want as some of the contents are lost. can anyone please help how to do it?? e.g. if I have a file which has the contents "This is the test string which needs to be modified". I want to accept this string as input stream and want to modify the contents to "This is the test string which is modified" , ( by replacing 'needs to be' with is).

    public static void main(String[] args) {
        String string = "This is the test string which needs to be modified";
        InputStream inpstr = new ByteArrayInputStream(string.getBytes());
           //Code to do


    }

In this I want the output as: This is the test string which is modified

Thanking you in advance.

like image 355
ranjan Avatar asked May 02 '12 07:05

ranjan


People also ask

How do you change input stream to String?

To convert an InputStream Object int to a String using this method. Instantiate the Scanner class by passing your InputStream object as parameter. Read each line from this Scanner using the nextLine() method and append it to a StringBuffer object. Finally convert the StringBuffer to String using the toString() method.

How do you replace a part of a String in Java?

The Java string replace() method will replace a character or substring with another character or string. The syntax for the replace() method is string_name. replace(old_string, new_string) with old_string being the substring you'd like to replace and new_string being the substring that will take its place.

How do you replace a substring in a String in Java without using replace method?

To replace a character in a String, without using the replace() method, try the below logic. Let's say the following is our string. int pos = 7; char rep = 'p'; String res = str. substring(0, pos) + rep + str.


1 Answers

If the text to be changed will always fit in one logical line, as I stated in comment, I'd go with simple Line Reading (if applyable) 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) {
            // Apply regex on buf

            // build output
        }
    }
}

However I've always like to use inheritance so I'd define this somewhere:

class MyReader extends BufferedReader {
    public MyReader(Reader in)
    {
        super(in);
    }

    @Override
    public String readLine() throws IOException {
        String lBuf = super.readLine();
        // Perform matching & subst on read string
        return lBuf;
    }
}

And use MyReader in place of standard BufferedReader keeping the substitution hidden inside the readLine method.

Pros: substitution logic is in a specified Reader, code is pretty standard. Cons: it hides the substitution logic to the caller (sometimes this is also a pro, still it depends on usage case)

HTH

like image 98
BigMike Avatar answered Sep 27 '22 21:09

BigMike