A simple FilterReader that strips HTML tags out of a stream of characters : 缓冲字符读 « 文件输入输出 « Java

En
Java
1. 图形用户界面
2. 三维图形动画
3. 高级图形
4. 蚂蚁编译
5. Apache类库
6. 统计图
7. 
8. 集合数据结构
9. 数据类型
10. 数据库JDBC
11. 设计模式
12. 开发相关类
13. EJB3
14. 电子邮件
15. 事件
16. 文件输入输出
17. 游戏
18. 泛型
19. GWT
20. Hibernate
21. 本地化
22. J2EE平台
23. 基于J2ME
24. JDK-6
25. JNDI的LDAP
26. JPA
27. JSP技术
28. JSTL
29. 语言基础知识
30. 网络协议
31. PDF格式RTF格式
32. 映射
33. 常规表达式
34. 脚本
35. 安全
36. Servlets
37. Spring
38. Swing组件
39. 图形用户界面
40. SWT-JFace-Eclipse
41. 线程
42. 应用程序
43. Velocity
44. Web服务SOA
45. 可扩展标记语言
Java 教程
Java » 文件输入输出 » 缓冲字符读屏幕截图 
A simple FilterReader that strips HTML tags out of a stream of characters
 
 
/*
 * Copyright (c) 2004 David Flanagan.  All rights reserved.
 * This code is from the book Java Examples in a Nutshell, 3nd Edition.
 * It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied.
 * You may study, use, and modify it for any non-commercial purpose,
 * including teaching and use in open-source projects.
 * You may distribute it non-commercially as long as you retain this notice.
 * For a commercial use license, or to purchase the book, 
 * please visit http://www.davidflanagan.com/javaexamples3.
 */

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FilterReader;
import java.io.IOException;
import java.io.Reader;

/**
 * A simple FilterReader that strips HTML tags (or anything between pairs of
 * angle brackets) out of a stream of characters.
 */
public class RemoveHTMLReader extends FilterReader {
  /** A trivial constructor. Just initialize our superclass */
  public RemoveHTMLReader(Reader in) {
    super(in);
  }

  boolean intag = false// Used to remember whether we are "inside" a tag

  /**
   * This is the implementation of the no-op read() method of FilterReader. It
   * calls in.read() to get a buffer full of characters, then strips out the
   * HTML tags. (in is a protected field of the superclass).
   */
  public int read(char[] buf, int from, int lenthrows IOException {
    int numchars = 0// how many characters have been read
    // Loop, because we might read a bunch of characters, then strip them
    // all out, leaving us with zero characters to return.
    while (numchars == 0) {
      numchars = in.read(buf, from, len)// Read characters
      if (numchars == -1)
        return -1// Check for EOF and handle it.

      // Loop through the characters we read, stripping out HTML tags.
      // Characters not in tags are copied over previous tags
      int last = from// Index of last non-HTML char
      for (int i = from; i < from + numchars; i++) {
        if (!intag) { // If not in an HTML tag
          if (buf[i== '<')
            intag = true// check for tag start
          else
            buf[last++= buf[i]// and copy the character
        else if (buf[i== '>')
          intag = false// check for end of tag
      }
      numchars = last - from// Figure out how many characters remain
    // And if it is more than zero characters
    return numchars; // Then return that number.
  }

  /**
   * This is another no-op read() method we have to implement. We implement it
   * in terms of the method above. Our superclass implements the remaining
   * read() methods in terms of these two.
   */
  public int read() throws IOException {
    char[] buf = new char[1];
    int result = read(buf, 01);
    if (result == -1)
      return -1;
    else
      return (intbuf[0];
  }

  /** The test program: read a text file, strip HTML, print to console */
  public static void main(String[] args) {
    try {
      if (args.length != 1)
        throw new IllegalArgumentException("Wrong number of args");
      // Create a stream to read from the file and strip tags from it
      BufferedReader in = new BufferedReader(new RemoveHTMLReader(new FileReader(args[0])));
      // Read line by line, printing lines to the console
      String line;
      while ((line = in.readLine()) != null)
        System.out.println(line);
      in.close()// Close the stream.
    catch (Exception e) {
      System.err.println(e);
      System.err.println("Usage: java RemoveHTMLReader$Test" " <filename>");
    }
  }
}

   
  
Related examples in the same category
1. 从System.in创建BufferedReader
2. 从InputStreamReader创建BufferedReader
3. 从StringReader创建BufferedReader
4. 创建BufferedReader的FileReader
5. 从网址创建BufferedReader
6. Create BufferedReader from FileReader and Read / display lines from file
7. Create BufferedReader from InputStreamReader and System.in, read console input
8. 使用BufferedReader读取和处理行
9. 标签过滤器:转换标签以空格字符
10. List of lines in a file with BufferedReader
11. 使用BufferedReader读取一行
12. 使用BufferedReader读取用户输入的号码
13. 读行文字从文件与BufferedReader类
14. 读取文件使用BufferedReader
15. 读以逗号分隔的文件到一个数组
16. 阅读文件的内容
17. 读文字从文件
18. 读取文本文件
19. Call the static method PressAnykey to keep to "DOS" window open.
20. A standalone program that reads a list of classes and builds a database of packages, classes, and class fields and methods
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.