Java IP总时间

Java IP总时间,java,string,ip,Java,String,Ip,各位,我是Java新手,请帮助或指导我,我从文件中输入以下字符串: **IP** **Date** (1.39.15.91 ,30/Sep/2014,18:45:17 ) (1.39.15.91 ,30/Sep/2014,18:45:24 ) (1.39.15.91 ,30/Sep/2014,18:45:26 ) (173.0.48.47 ,30/Sep/2014,18:41:48 ) (69.12.70.89 ,30/Sep/2014,18:20:07 )

各位,我是Java新手,请帮助或指导我,我从文件中输入以下字符串:

        **IP**            **Date**
(1.39.15.91 ,30/Sep/2014,18:45:17 )
(1.39.15.91 ,30/Sep/2014,18:45:24 )
(1.39.15.91 ,30/Sep/2014,18:45:26 )
(173.0.48.47 ,30/Sep/2014,18:41:48 )
(69.12.70.89 ,30/Sep/2014,18:20:07 )
(119.18.55.52 ,30/Sep/2014,18:05:30 )
(119.18.55.52 ,30/Sep/2014,18:30:58 )
(119.18.55.52 ,30/Sep/2014,18:30:59 )
我正在尝试获取IP所花费的总时间

预期产出:

 1.39.15.91   date   total time spent of this ip(18:45:17-18:45:26)
 173.0.48.47  date   total time spent of this ip ( zero )
 69.12.70.89  date   total time spent of this ip (zero )
 119.18.55.52  date  total time spent of this ip(18:05:30-18:30:59)
我尝试的是:

       String sCurrentLine;

        br = new BufferedReader(new FileReader("D:\\test.txt"));

        while ((sCurrentLine = br.readLine()) != null) {

            //String str=value.toString();
              String[] tokens = sCurrentLine.split(","); //split into Words
              System.out.println(sCurrentLine);
        }

使用逗号作为分隔符将其拆分为子字符串

如果需要,可以删除逗号


当您尝试用逗号分隔每一行时,您的思路是正确的,但是您需要做更多的工作才能开始。在下面的代码中,我逐行解析您的输入文件,使用HashMap存储每个IP地址的最大和最小日期对象:

Map<String, Date> minMap = new HashMap<String, Date>();
Map<String, Date> maxMap = new HashMap<String, Date>();
String sCurrentLine;
br = new BufferedReader(new FileReader("D:\\test.txt"));

while ((sCurrentLine = br.readLine()) != null) {
    //String str=value.toString();
    sCurrentLine = sCurrentLine.substring(1, sCurrentLine.length - 1);
    String[] tokens = sCurrentLine.split(","); //split into words
    // (173.0.48.47 ,30/Sep/2014,18:41:48 )
    String timeStamp = tokens[1] + " " tokens[2];

    try {
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MMM/yyyy hh:mm:ss");
        Date parsedDate = dateFormat.parse(timeStamp);
        if (minMap.get(tokens[0]) == null) {
            minMap.put(tokens[0], parsedDate);
            maxMap.put(tokens[0], parsedDate);
        } else if (parsedDate.compareTo(minMap.get(tokens[0]) < 0) {
            minMap.put(tokens[0], parsedDate);
        } else if (parsedDate.compareTo(minMap.get(tokens[0]) > 0) { {
            maxMap.put(tokens[0], parsedDate);
        }
    } catch(Exception e){
    }
}

// iterate over max and min Maps and print result for each IP address
Set<Map.Entry<String, Date>> entrySet = minMap.entrySet();
for (Entry entry : entrySet) {
    String message = entry.getKey() + " date total time spent of this ip (";
    if (entry.getValue().compareTo(maxMap.get(entry.getKey()) == 0) {
        message = "zero";
    else {
        message += minMap.get(entry.getKey()) + "-" + maxMap.get(entry.getKey());
    }
    message += ")";
}

在上面的示例输出中,省略了时间戳范围中的日期,但实际上可能有不同的日期,因此我在代码中包含了这一点。另外,如果您想以毫秒或其他格式获得两个时间戳之间的差异,还需要做更多的工作。

到目前为止您做了什么?您至少能从java文件中获取数据吗?请看一下,请给出您期望从上述输入数据中获得的输出?@user1354678我正在读取文件,并从空间中分离出IP和日期,然后我该怎么做??我不知道;您的编辑未显示所花费的总时间