根据行中包含的数字对文件中的行进行排序

时间:2014-03-25 02:35:12

标签: java

我有以下文本文件(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中的“sort”函数可以帮助我排序。 所以我的想法是提取第5行中的所有数字,将它们保存在向量中然后对向量进行排序并找到将数字与特定行相关联的方法,这样一旦对数字进行排序,行也会被排序,然后保存他们到另一个文件。关于如何编程的任何想法?

这是我到目前为止的程序:

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);

    }

3 个答案:

答案 0 :(得分:1)

创建一个包含两个字段numberline的值对象bean类。实施comparable并覆盖该类中的compareTo方法。扫描文件时填充此bean类的ArrayList。然后对ArrayList

进行排序

答案 1 :(得分:0)

如何使用String.split

String [] arr = currentLine.split ("\\s+");

尝试使用treeMap进行存储

http://docs.oracle.com/javase/6/docs/api/java/util/TreeMap.html

答案 2 :(得分:0)

使用TreeMap存储与特定行中最后一个数字相对应的行

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);
}

最后,您可以打印并查看结果(或者您可以根据需要将其写入文件):

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

上面没有捕获重复的行,更新结构以捕获它

将地图与Arraylist一起使用,以存储与特定数字相对应的行

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);