java 替换输入流中的字符串段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10409511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 00:56:17  来源:igfitidea点击:

replace a string segment from input stream

javainputstream

提问by ranjan

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).

我正在尝试接收一个巨大的文本文件作为输入流,并希望将一个字符串段转换为另一个字符串。我对如何做到这一点感到非常困惑,如果我将整个输入流转换为我不想要的字符串,它会很好地工作,因为某些内容丢失了。任何人都可以请帮助如何做到这一点?例如,如果我有一个包含“这是需要修改的测试字符串”内容的文件。我想接受这个字符串作为输入流,并希望将内容修改为“这是被修改的测试字符串”,(通过用 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.

提前致谢。

回答by BigMike

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.

并使用 MyReader 代替标准的 BufferedReader 将替换隐藏在 readLine 方法中。

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

HTH

回答by Nikolay Antipov

May be I understood you wrong, but I think you should build a stack machine. I mean you can use a small string stack to collect text and check condition of replacement.

可能我理解错了,但我认为你应该建立一个堆栈机器。我的意思是您可以使用一个小的字符串堆栈来收集文本并检查替换条件。

If just collected stack already is not matched to your condition, just flush stack to output and collect it again.

如果刚刚收集的堆栈已经与您的条件不匹配,只需刷新堆栈以输出并再次收集它。

If your stack is similar with condition, carry on collecting it.

如果您的堆栈与条件相似,请继续收集。

If your stack is matched your condition, make a modification and flush modified stack to output.

如果您的堆栈符合您的条件,请进行修改并将修改后的堆栈刷新到输出。