集册 Java实例教程 对文件内容应用正则表达式

对文件内容应用正则表达式

欢马劈雪     最近更新时间:2020-01-02 10:19:05

471
在文件内容上应用正则表达式


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();


    
展开阅读全文