Java 提取整数部分值,该值是给定文件中唯一的ID

Java 提取整数部分值,该值是给定文件中唯一的ID,java,Java,我已经写了一个程序来提取文件中的所有整数值,并找到重复的整数。但我只想要那些像ID=“******..”/ID=“******..”一样的整数值。我不想考虑依赖于“< /代码>值不管它是什么。< /P> 我的文件是:例如 <line id="24867948" dependsOnPresenceOf="7417840"> <element text="Card Balance " id="18829409" dependsOnPresenceOf="

我已经写了一个程序来提取文件中的所有整数值,并找到重复的整数。但我只想要那些像
ID=“******..”/ID=“******..”
一样的整数值。我不想考虑<代码>依赖于“< /代码>值不管它是什么。< /P> 我的文件是:例如

  <line id="24867948" dependsOnPresenceOf="7417840">
            <element text="Card Balance " id="18829409" dependsOnPresenceOf="28696224" />
  <line id="2597826922" dependsOnPresenceOf="200114712343">
            <methodElement fixedWidth="17" precededBySpace="false" id="418710522">
  <line id="24867948" dependsOnPresenceOf="10565536">
            <element text="  Cert. Number:" id="23917950" dependsOnPresenceOf="10565536" />
  <line id="24867948" dependsOnPresenceOf="10565536">
        <element text="  Cert. Number:" id="23917950" dependsOnPresenceOf="10565536" />

下面是我编写的仅用于提取整数值的程序:

public class DuplicateIDPicker {

protected static final Logger logger = Logger
        .getLogger(com.aspire.pos.DuplicateIDPicker.class);

public static String finalContent = "";

public static void main(String[] args) throws FileNotFoundException {

    String content = "";
    /* Set location of the file as format below */
    String path = "D://TASK/DuplicateFinder/OriginalFile/";
    /* Set file name to be evaluate with extension */
    String fileName = "SSLItems.bpt";

    File f = new File(path.concat(fileName));

    try {
        content = readFile(f);
        String extractedInteger = content.replaceAll("\\D+", " ");
        String[] arrayOfID = findAllIDInArray(extractedInteger);

        System.out.println("***********************");

        HashSet<String> set    = new HashSet<String>();
        HashSet<String> newSet = new HashSet<String>();
        System.out.println("Duplicate ID's found :");
        for (String arrayElement : arrayOfID) {
            if (!set.add(arrayElement)) {
                // System.out.println("Duplicate Element is : "+arrayElement);
                newSet.add(arrayElement);
            }
        }

        System.out.println("-----------------------");
        /* here are all Duplicate Id */
        System.out.println(newSet);

    } catch (IOException e) {
        logger.error(e.getMessage());
    }

}

@SuppressWarnings("resource")
public static String readFile(File f) throws IOException {
    String data = "";

    FileReader fr = new FileReader(f);
    BufferedReader br = new BufferedReader(fr);

    while ((data = br.readLine()) != null) {
        // Print the content on the console
        finalContent = finalContent + data;
    }

    return finalContent;
}

public static String[] findAllIDInArray(String str) {
    String[] value = str.split(" ");
    return value;
}
}
公共类复制器{
受保护的静态最终记录器=记录器
.getLogger(com.aspire.pos.DuplicateIDPicker.class);
公共静态字符串finalContent=“”;
公共静态void main(字符串[]args)引发FileNotFoundException{
字符串内容=”;
/*将文件的位置设置为以下格式*/
String path=“D://TASK/DuplicateFinder/OriginalFile/”;
/*设置要使用扩展名计算的文件名*/
字符串fileName=“SSLItems.bpt”;
文件f=新文件(path.concat(文件名));
试一试{
内容=读取文件(f);
String extractedInteger=content.replaceAll(“\\D+”,”);
字符串[]arrayOfID=findAllIDInArray(extractedInteger);
System.out.println(“****************************”);
HashSet=newhashset();
HashSet newSet=新HashSet();
System.out.println(“发现重复ID:”);
for(字符串数组元素:arrayOfID){
如果(!set.add(arrayElement)){
//System.out.println(“重复元素为:“+arrayElement”);
新闻集添加(数组元素);
}
}
System.out.println(“--------------------------”;
/*这些都是重复的身份证*/
System.out.println(新闻集);
}捕获(IOE异常){
logger.error(例如getMessage());
}
}
@抑制警告(“资源”)
公共静态字符串读取文件(文件f)引发IOException{
字符串数据=”;
FileReader fr=新的FileReader(f);
BufferedReader br=新的BufferedReader(fr);
而((data=br.readLine())!=null){
//在控制台上打印内容
最终内容=最终内容+数据;
}
返回最终内容;
}
公共静态字符串[]findAllIDInArray(字符串str){
字符串[]值=str.split(“”);
返回值;
}
}

您可以执行
content.replaceAll(“dependsonpresence=\”\\d+\”,“”)
要删除这些不需要的字符串,可以执行
content.replaceAll(“dependsonPresence=\”\\d+\”,“”)
删除这些不需要的字符串

这是解决我的问题的另一个解决方案。
  This one is another solution to solve my problem.

    public class DuplicateIDPicker {
    protected static final Logger logger = Logger
                .getLogger(com.aspire.pos.DuplicateIDPickerOld.class);

    public static String finalContent = "";

    public static void main(String[] args) throws FileNotFoundException,
                IOException {

        String content = "";
        String[] arrayOFId = {};
        String[] listOfID = {};
        HashSet<String> set = new HashSet<String>();
        HashSet<String> newSet = new HashSet<String>();

        /* Set location of the file as format below */
        String path = "D://TASK/DuplicateFinder/OriginalFile/";
        /* Set file name to be evaluate with extension */
        String fileName = "SSLPickupDeliveryOrderReceipt.txt";

        File f = new File(path.concat(fileName));

        content = readFile(f);
        arrayOFId = findAllIDInString(content);
        listOfID = extractIDOnly(arrayOFId);

        System.out.println("***********************");

        System.out.println("Duplicate ID's found :");
        for (String arrayElement : listOfID) {
            if (!set.add(arrayElement)) {
                newSet.add(arrayElement);
            }
        }

        System.out.println("-----------------------");
        /* Duplicate Id stored in a Set : */
        System.out.println(newSet);

    }

    /*
     * This method is implemented to read file and 
     * return content in String format
     */
    public static String readFile(File f) {
        String data = "";
        try {
            FileReader fr = new FileReader(f);
            BufferedReader br = new BufferedReader(fr);

            while ((data = br.readLine()) != null) {
                finalContent = finalContent + data;
            }
            br.close();
        } catch (IOException e) {
            logger.error(e.getMessage());
        }
        return finalContent;
    }


    /*
     * This method is implemented to get Array string 
     * on the basis of '"'
     */
    public static String[] extractIDOnly(String[] arr) {
        ArrayList<String> listOfID = new ArrayList<String>();
        for (int i = 0; i < arr.length; i++) {
            String newString = arr[i];
            String[] finalString = {};
            for (int j = 0; j < newString.length();) {
                finalString = newString.split("\"", 3);
                for (int k = 1; k < finalString.length;) {
                    listOfID.add(finalString[1]);
                    break;
                }
                break;
            }

        }
        return (String[]) listOfID.toArray(new String[listOfID.size()]);
    }

    /*
     * This method is implemented to split the ID part only
     */
    public static String[] findAllIDInString(String str) {
        String[] value = str.split("id=");
        return value;
    }
}
公共类复制器{ 受保护的静态最终记录器=记录器 .getLogger(com.aspire.pos.DuplicateIDPickerOld.class); 公共静态字符串finalContent=“”; 公共静态void main(字符串[]args)引发FileNotFoundException, IOException{ 字符串内容=”; 字符串[]arrayOFId={}; 字符串[]listOfID={}; HashSet=newhashset(); HashSet newSet=新HashSet(); /*将文件的位置设置为以下格式*/ String path=“D://TASK/DuplicateFinder/OriginalFile/”; /*设置要使用扩展名计算的文件名*/ String fileName=“sslpickupdeliveryorderreceive.txt”; 文件f=新文件(path.concat(文件名)); 内容=读取文件(f); arrayOFId=FindAlliInstring(内容); listOfID=仅提取(arrayOFId); System.out.println(“****************************”); System.out.println(“发现重复ID:”); for(字符串数组元素:listOfID){ 如果(!set.add(arrayElement)){ 新闻集添加(数组元素); } } System.out.println(“--------------------------”; /*存储在集合中的重复Id:*/ System.out.println(新闻集); } /* *此方法用于读取文件和 *以字符串格式返回内容 */ 公共静态字符串读取文件(文件f){ 字符串数据=”; 试一试{ FileReader fr=新的FileReader(f); BufferedReader br=新的BufferedReader(fr); 而((data=br.readLine())!=null){ 最终内容=最终内容+数据; } br.close(); }捕获(IOE异常){ logger.error(例如getMessage()); } 返回最终内容; } /* *实现此方法是为了获取数组字符串 *在‘’的基础上 */ 公共静态字符串[]extractIDOnly(字符串[]arr){ ArrayList listOfID=新的ArrayList(); 对于(int i=0;i
这是解决我问题的另一个解决方案。
公共类复制器{
受保护的静态最终记录器=记录器
.getLogger(com.aspire.pos.DuplicateIDPickerOld.class);
公共静态字符串finalContent=“”;
公共静态void main(字符串[]args)引发FileNotFoundException,
IOException{
字符串内容=”;
字符串[]arrayOFId={};
字符串[]listOfID={};
HashSet=newhashset();
HashSet newSet=新HashSet();
/*设置t的位置
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DuplicateIDPicker
{
    public static void main(String[] args)
    {
        /* Set location of the file as format below */
        String path = "C://Temp/";
        /* Set file name to be evaluate with extension */
        String fileName = "in.txt";

        Set<String> all = new HashSet<>();
        Set<String> duplicates = new HashSet<>();

        String regex = "(id|ID)\\=\"" // attribute name + quoted equal and quotation
                + "(\\d+)"  // id value marked as (capturing group)
                + "\"";     // closing quotation
        try {
            String content = readFile(path + fileName);
            Matcher m = Pattern.compile(regex).matcher(content);
            while (m.find()) {
                String idValue = m.group(2);
                if (!all.add(idValue)) duplicates.add(idValue);
            }
            System.out.println(duplicates);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String readFile(String fileFullPath) throws IOException
    {
        return new String(Files.readAllBytes(Paths.get(fileFullPath)));
    }
}