我有以下文本文件(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