⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 e425. applying regular expressions on the contents of a file.txt

📁 这里面包含了一百多个JAVA源文件
💻 TXT
字号:
The matching routines in java.util.regex require that the input be a CharSequence object. This example implements a method that efficiently returns the contents of a file in a CharSequence object. 
    // Converts the contents of a file into a CharSequence
    // suitable for use by the regex package.
    public CharSequence fromFile(String filename) throws IOException {
        FileInputStream fis = new FileInputStream(filename);
        FileChannel fc = fis.getChannel();
    
        // Create a read-only CharBuffer on the file
        ByteBuffer bbuf = fc.map(FileChannel.MapMode.READ_ONLY, 0, (int)fc.size());
        CharBuffer cbuf = Charset.forName("8859_1").newDecoder().decode(bbuf);
        return cbuf;
    }

Here is sample code that uses the method: 
    try {
        // Create matcher on file
        Pattern pattern = Pattern.compile("pattern");
        Matcher matcher = pattern.matcher(fromFile("infile.txt"));
    
        // Find all matches
        while (matcher.find()) {
            // Get the matching string
            String match = matcher.group();
        }
    } catch (IOException e) {
    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -