2014-03-25 60 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