📜  从 Java 的 .class 文件中提取内容的Java程序

📅  最后修改于: 2022-05-13 01:54:36.185000             🧑  作者: Mango

从 Java 的 .class 文件中提取内容的Java程序

在本文中,我们将使用Apache Tika库提取Java类文件的内容。 Apache Tika 用于从各种文件格式中进行文档类型检测和内容提取。它使用各种文档解析器和文档类型检测技术来检测和提取数据。它提供了一个用于解析不同文件格式的通用 API。所有这些解析器库都封装在一个称为解析器接口的接口中。

下表显示了解决方案中使用的重要方法的描述:

Method

Description

BodyContentHandler()It creates a content handler that writes XHTML body character events to an internal string buffer.
Metadata() It constructs new, empty metadata.
ParseContext()It creates a parse context object that is used to pass context information to Tika parsers.
parse()Instantiate the parser object, and invoke the parse method.

示例:提取Java类文件内容的Java代码格式

Java
// Java program to extract the
// contents of Java class file
// format
  
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
  
// importing Apache Tika libraries
import org.apache.tika.exception.TikaException;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.parser.AutoDetectParser;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.parser.Parser;
import org.apache.tika.sax.BodyContentHandler;
  
import org.xml.sax.SAXException;
  
public class ParserExtraction {
  
    public static void main(final String[] args)
        throws IOException, SAXException, TikaException
    {
        // create a File object
        File f = new File("AddTwoNumbers.java");
  
        // parse method parameters
        Parser parser = new AutoDetectParser();
  
        // instantiate BodyContentHandle
        BodyContentHandler handler
            = new BodyContentHandler();
  
        // Creates the Metadata object
        Metadata metadata = new Metadata();
  
        FileInputStream inputstream
            = new FileInputStream(f);
  
        // creates a parse context object
        ParseContext context = new ParseContext();
  
        // parsing the file
        parser.parse(inputstream, handler, metadata,
                     context);
            
          // display the file content
        System.out.println("File content : "
                           + Handler.toString());
    }
}


输入 :

使用 Apache Tika 提取 Java 程序

添加两个数字。Java

输出:

File content :  public class AddTwoNumbers {

  public static void main(String[] args) {
       
     int num1 = 5, num2 = 15, sum;
     sum = num1 + num2;

     System.out.println("Sum of these numbers: "+sum);
  }
}