2014-03-25 62 views
0

我有以下文本文件(offline.txt):排序行包括在行

# Timestamp, X, Y, MAC Address of AP, RSS 
1395444273179 35.19967269897461 19.1965389251709 28:c6:8e:85:80:d3 -71 
1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:8e:e9:a1 -75 
1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:8e:e9:a2 -74 
1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:8e:e9:b1 -84 
1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:8e:e9:b2 -85 
1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:8e:e9:b0 -85 
1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:8e:e9:a0 -74 
1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:87:04:41 -75 
1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:87:04:40 -73 
1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:87:04:42 -74 
1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:87:04:52 -96 
1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:87:04:50 -97 

我想根據在數目的文件的行進行排序文件的第5列按降序排列,如果重複一個值,則重複值的順序無關緊要。

例如,這是所需的輸出(offline_out.txt)我想上一個特別的文本文件:

# Timestamp, X, Y, MAC Address of AP, RSS 
1395444273179 35.19967269897461 19.1965389251709 28:c6:8e:85:80:d3 -71 
1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:87:04:40 -73 
1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:87:04:42 -74 
1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:8e:e9:a0 -74 
1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:8e:e9:a2 -74 
1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:8e:e9:a1 -75 
1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:8e:e9:b1 -84 
1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:8e:e9:b2 -85 
1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:8e:e9:b0 -85 
1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:87:04:52 -96 
1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:87:04:50 -97 

我知道如何讀取這個文件,我知道了「排序」功能java可以幫助我排序。 所以我的想法是提取第五行中的所有數字,將它們保存在一個向量中,然後對向量進行排序並找到一種方法將數字指定給特定的行,因此一旦數字被排序,行也被排序,然後將它們保存到另一個文件。關於如何編程的任何想法?

這是程序我到目前爲止:

public class extract { 
    public static void main (String[] args) throws java.lang.Exception 
    { 
    File inputFile = new File("offline.txt"); 
    File tempFile = new File("offline_out.txt"); 

    BufferedReader reader = new BufferedReader(new FileReader(inputFile)); 
    BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile)); 

    //while to read all the lines, but how can I store only the numbers to a vector an associate them to a specific row? 
     while((currentLine = reader.readLine()) != null) { 
      } 
    } 

    //to save the output file 
    boolean successful = tempFile.renameTo(inputFile); 

    } 
+0

我認爲你有一個好方法。分別研究你的方法中的每一步,你應該達到你想要的結果。我建議你將自己的方法中的每一步都打入自己的方法。 – Bernard

+0

也許你可以將每行讀入一個對象,然後使用最後一列的「比較器」對對象列表進行排序? –

+0

如果我將每一行作爲一個對象,我如何對「比較器」說,這些行應根據第5列中的數字進行排序? – user3349667

回答

0

使用一個TreeMap來存儲對應於las的行在特定線路

TreeMap<Integer, String> map = new TreeMap<Integer, String>(); 
while((currentLine = reader.readLine()) != null) { 

    // split the line and use the last value as key 
    if (!currentLine.contains("Timestamp")) 
     map.put(Integer.parseInt(currentLine.split("\\s+")[4]), currentLine); 
    else 
     map.put(0, currentLine); 
} 

最後,你可以打印和看到的結果(或者你可以寫一個文件,請你)T編號:

for(Integer key : map.descendingKeySet()) 
    System.out.println(map.get(key)); 

重複的線路不被捕獲在上述中,更新所述結構以捕獲它

使用地圖與Arraylist,存儲對應於所述特定數目

0123的線
TreeMap<Integer, ArrayList<String>> map = new TreeMap<Integer, ArrayList<String>>(); 
while((currentLine = reader.readLine()) != null) { 

    int key; 
    if (!currentLine.contains("Timestamp")) 
     // split the line and use the last value as key 
     key = Integer.parseInt(currentLine.split("\\s+")[4]; 
    else 
     key = 0; 
    ArrayList<String> lines; 
    if (!map.contains(key)) //if the key doesn't exist create a new arraylist 
     lines = new ArrayList<String>();    
    else // if the key exists use the arraylist in the map 
     lines = map.get(key); 
    lines.add(currentLine); 
    map.put(key, lines); 
} 

和用於打印:

for(Integer key : map.descendingKeySet()) 
    for(String line : map.get(key)) 
     System.out.println(line); 
+0

謝謝你部分工作,問題是它忽略重複的行。 – user3349667

+0

在地圖中,按鍵將是唯一的,這就是重複行將被忽略的原因。在重複行的情況下,你是否有任何分類偏好? – AKS

+0

看看更新的答案。 – AKS

1

創建具有兩個字段,numberline值對象bean類。執行comparable並覆蓋該類中的compareTo方法。在掃描文件時填充此Bean類的ArrayList。然後對ArrayList進行排序。

+0

或@ shree.pat18註釋的更新答案,如果您認爲您想按其他值排序,則使用「比較器」。不過,我認爲'Comparable'在這裏可以 – MadcoreTom