JAVA在文本文件中显示重复的某些值 [英] JAVA show the duplicate certain values in a text file

查看:74
本文介绍了JAVA在文本文件中显示重复的某些值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[2014-08-19 00:01:43,453] REC | SRC:923119511824 | DST:9900 | CNT:1 | OPR:zong | ADD RIDAsMs

[2014-08-19 00 :01:32,894] REC | SRC:923142676343 | DST:9900 | CNT:1 | OPR:zong | ADD BBCMEDIA \ n \\ nn

[2014-08-19 00:02:42,754 ] REC | SRC:923119511824 | DST:9900 | CNT:1 | OPR:zong | ADD sMs



这里是文本文件中的文字,我想显示SRC:之后的重复联系号码与java中ADD之后显示的联系号码。任何人都可以帮忙吗???我已经阅读了文件并将其存储到arraylist中,我是java的初学者。

[2014-08-19 00:01:43,753] REC|SRC:923119511824|DST:9900|CNT:1|OPR:zong|ADD RIDAsMs
[2014-08-19 00:01:32,894] REC|SRC:923142676343|DST:9900|CNT:1|OPR:zong|ADD BBCMEDIA\n\n
[2014-08-19 00:02:42,754] REC|SRC:923119511824|DST:9900|CNT:1|OPR:zong|ADD sMs

here is the text in text file, i want to show the duplicate contact numbers which are after "SRC:" with the contant that is shown after the "ADD" in java. Can any one help please??? i have read the file and stored it into an arraylist, i am beginner to java.

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class pring
{
    private static ArrayList<string> data = new ArrayList<string>();
    public static void main(String[] args) throws IOException
    {
        arrayListConstructor("C:/Users/DfroJaCk DB/Desktop/zongrecv.txt");
        System.out.println(data);
    }
    public static void arrayListConstructor(String filename) throws IOException
    {
        BufferedReader br = null;
        br = new BufferedReader(new FileReader(filename));
        String line = br.readLine();
        while (line != null)
        {
            data.add(line);
            line = br.readLine();
        }
        br.close();
    }
}

推荐答案

使用 String.split()方法,如文档中所述 [ ^ ]。然后,您可以比较记录的特定字段。
Use on of the String.split() methods, as described in the documentation[^]. You can then compare specific fields of the records.


使用HashMap:



Use HashMap:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

public class pring
{
    private static ArrayList<String> data = new ArrayList<String>();
    public static void main(String[] args) throws IOException
    {
        arrayListConstructor("C:/Users/DfroJaCk DB/Desktop/zongrecv.txt");
        System.out.println(data);
    }
    public static void arrayListConstructor(String filename) throws IOException
    {
        HashMap<String, String> map = new HashMap<String, String>();

        BufferedReader br = null;
        br = new BufferedReader(new FileReader(filename));
        String line = br.readLine();
        while (line != null)
        {
            /* if line is new key, return null,
             * if line is duplicate key, return value
             */
            if (map.get(line) == null) {
                System.out.println("new: " + line);
                data.add(line);
                map.put(line, line);
            }
            else
                System.out.println("duplicate: " + line);

            line = br.readLine();
        }
        br.close();
    }
}


这篇关于JAVA在文本文件中显示重复的某些值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆