首页 > 解决方案 > Java中是否有任何内置函数可以从提取的数据中删除不需要的数据

问题描述

我从文本文件中提取了一些文本,但现在我只想要该文本中的一些特定单词。

我尝试过的是从该文本文件中读取的,并且我使用关键字进行了搜索:

    FileReader fr = new 
    FileReader("D:\\PDFTOEXCEL\\Extractionfrompdf.txt");
    BufferedReader br = new BufferedReader(fr);
    String s;

    String keyword = "dba COPIEFacture ";

    while ((s = br.readLine()) != null) {
        if (s.contains(keyword)) {
            System.out.println(s);

我得到这样的输出:dba COPIEFacture du 28/05/2018 n° 10077586115Récapitulatif de vote facture

但我只想要 28/05/2018 这个所以请帮助我

标签: java

解决方案


这会成功的。

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Main {

   public static void main(String[] args) {

    FileReader fr;
    String keyword = "dba COPIEFacture du ";
     String textToFind = "28/05/2018"; // The length usually will not 
                                       // change.You can use value 
                                       // 10(length) instead
    StringBuilder sb = new StringBuilder();
    try {
        fr = new FileReader("D:\\PDFTOEXCEL\\Extractionfrompdf.txt");

        int i;
        while ((i = fr.read()) != -1) {
            sb.append((char) i);
        }

        int start = sb.indexOf(keyword) + keyword.length();
        int end = start + textToFind.length();

        System.out.print(sb.substring(start, end));   //output: 28/05/2018

        fr.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
   }
 }

推荐阅读