我有以下文本文件(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
我想根据文件第五列中的数字按降序对文件行进行排序,如果某个值重复,则重复值的顺序并不重要。
例如,这是我想要的上一个特定文本文件的所需输出(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中的“排序”功能可以帮助我排序。 所以我的想法是提取第五行中的所有数字,将它们保存在一个 vector 中,然后对 vector 进行排序,并找到一种将数字与特定行相关联的方法,这样一旦数字被排序,行也会被排序,然后保存它们到另一个文件。关于如何编程有什么想法吗?
这是我到目前为止的程序:
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);
}
最佳答案
创建一个具有两个字段的值对象 bean 类:number
和 line
。实现 comparable
并重写该类中的 compareTo
方法。当您扫描文件时,填充此 Bean 类的 ArrayList
。然后对ArrayList
进行排序。
关于java - 根据行中包含的数字对文件中的行进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22624454/