Java中io的问题

03-08-26 efan1
怎样用java在文本文件中快速的查找和替换内容。

谢谢了。

anonymous
2003-08-26 18:04
正则式呀!

import java.util.regex.*;

public class BasicReplace {

public static void main(String[] args) {

CharSequence inputStr = "a b c a b c";

String patternStr = "a";

String replacementStr = "x";

// Compile regular expression

Pattern pattern = Pattern.compile(patternStr);

// Replace all occurrences of pattern in input

Matcher matcher = pattern.matcher(inputStr);

String output = matcher.replaceAll(replacementStr);

// x b c x b c

}

}

猜你喜欢