使用类似的东西
public CharSequence fromFile(String filename) throws IOException {
FileInputStream input = new FileInputStream(filename);
FileChannel channel = input.getChannel();
// Create a read-only CharBuffer on the file
ByteBuffer bbuf = channel.map(FileChannel.MapMode.READ_ONLY, 0, (int)channel.size());
CharBuffer cbuf = Charset.forName("8859_1").newDecoder().decode(bbuf);
return cbuf;
}并比较为:
try {
// Create matcher on file
Pattern pattern = Pattern.compile("pattern");
Matcher matcher = pattern.matcher(fromFile("yourFile.txt"));
// Find all matches
while (matcher.find()) {
// Get the matching string
String match = matcher.group();
}
} catch (IOException e) {
}
博客展示了Java代码,通过FileInputStream和FileChannel读取文件内容,将其转换为CharBuffer。还使用Pattern和Matcher进行正则表达式匹配,在文件中查找符合特定模式的字符串,并获取匹配结果。


被折叠的 条评论
为什么被折叠?



