我有以下文本文件(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);
}
我認爲你有一個好方法。分別研究你的方法中的每一步,你應該達到你想要的結果。我建議你將自己的方法中的每一步都打入自己的方法。 – Bernard
也許你可以將每行讀入一個對象,然後使用最後一列的「比較器」對對象列表進行排序? –
如果我將每一行作爲一個對象,我如何對「比較器」說,這些行應根據第5列中的數字進行排序? – user3349667