2012-10-11 20 views
2

我正在查看文件名的日誌以及它們在指定日期上次修改的時間。其內容如下所示:對子串內的子串進行排序

(comment:file_02389.txt,lastmodified:Wed Oct 10 19:10:49) 
(comment:file_02342.txt,lastmodified:Wed Oct 10 17:16:08) 
(comment:file_02315.txt,lastmodified:Wed Oct 10 18:45:12) 
(comment:file_02344.txt,lastmodified:Wed Oct 10 08:31:01) 

日誌以單個字符串形式給出,沒有換行符。我想解析字符串來查找最近修改過的文件,即在這種情況下具有最新的日期file_02389.txt。每個「註釋」的字符長度是固定的,雖然假設將來可能會改變,並且如果同一個文件被多次修改,則文件名不會是唯一的。

是否有最可擴展/可維護的方法來查找最新的文件?執行時間和記憶不是重要的因素。主要的問題是初學者程序員可以理解和使用代碼。

我的第一個想法是將字符串拆分爲可以使用自定義比較器排序的列表。我認爲這是簡單的,但不能擴展:

{//given String log 
... 
//setup 
List<String> temp = Arrays.asList(log.trim().split("\\(comment\\:")); //too complex for one line? 
//the first entry is blank so it must be removed else a substring() call will fail 
if(temp.get(0).equals("")) 
    temp.remove(0); 
int period = full.get(0).indexOf('.'); 
int colon = full.get(0).indexOf(':'); 

//process 
Collections.sort(temp, DATE); 
return test.get(test.size()-1).substring(0, period)) //last entry is the most recent 
} 

public final Comparator<String> DATE = new Comparator<String>() 
{ 
public int compare(String s1, String s2) 
    { 
     return s1.substring(28).compareTo(s2.substring(28)); 
    } 
}; 

它的工作原理,但使用依賴於該線長度和比較,這只是在這一個案例有用子。我不喜歡使用.split,然後不得不刪除第一個條目,但是我想避免一個真實而難以理解的正則表達式,如果這是一個替代方案。將日期視爲字符串而不是比較爲整數或日期對象似乎不合乎要求,但可節省代碼行。

我目前使用的排序映射避免創建一個隨機數字的一次性使用的比較器,但專門的映射看起來相當複雜,我試圖做的事情。我仍然認爲比爲文件名創建一個數組更好,另一個時間創建數組,然後複製時間三分之一,以便可以對時間數組進行排序,並將其最後一個值與副本中的相應索引進行比較。

{ 
... 
//same setup as before 
//process 
//key is time, value is file name 
SortedMap<String, String> map = new TreeMap<String, String>(); 
for(String s : temp) 
    map.put(s.substring(colon+1), s.substring(0, period)); 
//the value to which the last key is mapped is guaranteed to be the most recent file 
return map.get(map.lastKey()); //too complex for one line? 
} 
+0

在你的例子中10月10日是週一。和週三。 - 不可能...其次 - 你使用字符串比較來比較日期 - 這不應該工作。 – alfasin

+0

我的錯字,改變週一至週三,謝謝。因爲日期被視爲字符串,所以比較起作用。 '10月10日19:10:49'週三大於'Wed Oct 10 17:16:08',因爲'9'大於'7'。顯然這隻有在格式一致並且不用於_how>或<日期時才適用。 – apcris

+0

沒有。它會一直持續到你開始比較不同的日子。 W> T因此你會得到那個Wend。 >週四etc. – alfasin

回答

2

會這樣的工作? 基本上解析每行的日期,建立一個「Pair」對象的集合,然後我可以根據日期對集合進行排序。

import java.text.ParseException; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.Collections; 
import java.util.Date; 
import java.util.List; 


public class Parse { 

    /** 
    * @param args 
    * @throws ParseException 
    */ 
    public static void main(String[] args) throws ParseException { 
     StringBuilder sb = new StringBuilder(); 
     sb.append("(comment:file_02389.txt,lastmodified:Wed Oct 10 19:10:49)").append("\n"); 
     sb.append("(comment:file_02342.txt,lastmodified:Wed Oct 10 17:16:08)").append("\n"); 
     sb.append("(comment:file_02315.txt,lastmodified:Wed Oct 10 18:45:12)").append("\n"); 
     sb.append("(comment:file_02344.txt,lastmodified:Wed Oct 10 08:31:01)").append("\n"); 

      //create a date format that can parse dates formatted in the file 
     SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss"); 

      //read the file into an array of lines (or read one line at a time) 
     String[] lines = sb.toString().split("\n"); 

      //create an array of pair objects to hold the line as well as the date 
     List<Pair> list = new ArrayList<Pair>(); 


     for(int i=0;i<lines.length;i++){ 
        //get the date component of the line 
      String dateString = lines[i].substring(lines[i].length()-20, lines[i].length()-1); 

      Pair pair = new Pair(); 
      pair.date = sdf.parse(dateString); 
      pair.line = lines[i]; 
      list.add(pair); 
     } 
     Collections.sort(list); 
     System.out.println(list.get(list.size()-1).line); 
    } 
} 
class Pair implements Comparable<Pair>{ 

    public Date date; 
    public String line; 

    @Override 
    public int compareTo(Pair o) { 
     return date.compareTo(o.date); 
    } 

} 
+0

它會起作用,似乎比我的方法更爲合乎邏輯的開始到結束。我不確定需要的日期格式或解析器是否內置。 – apcris

2

我感興趣的人會建議什麼,但我的第一本能是把你原來的字符串轉換成JSON數組(有幾個替換查詢)。然後,您可以反序列化該json並立即獲取對象實例的列表,每個實例都有兩個屬性,註釋和日期。

您可以爲這些對象設置一個比較器,按照您的需要對列表進行排序。

+0

這正是我想要建議的。方式更容易。 – dispake

+0

我不熟悉JSON,但使用了XML。我喜歡這種方法如何將方法的複雜性抽象出來,並且可能對其他應用程序有用。 – apcris

+1

對。 XML或JSON,都將工作。我傾向於更喜歡JSON,但是對於你之後的事情,它們真的是可以互換的。 – Lolo