在文件内容上应用正则表达式
import java.io.FileInputStream; /** NowJava.com - 时 代 Java **/ import java.io.IOException; import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.channels.FileChannel; import java.nio.charset.Charset; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { public void main(String[] argv) { try { Pattern pattern = Pattern.compile("pattern"); Matcher matcher = pattern.matcher(fromFile("infile.txt")); // Find all matches /* 来 自* NowJava.com - 时代Java */ while (matcher.find()) { // Get the matching string String match = matcher.group(); } } catch (IOException e) { } } public CharSequence fromFile(String filename) throws IOException { FileInputStream fis = new FileInputStream(filename); FileChannel fc = fis.getChannel();