如何在Java中对文件数据进行排序和重新排列?

如何在Java中对文件数据进行排序和重新排列?,java,loops,text,fileinputstream,Java,Loops,Text,Fileinputstream,我有一个以这种格式保存的txt文件: 806PYZnmNaw4h0wh8fyWDQ 0 0 0 0 0 1 0 bvu13GyOUwhEjPum2xjiqQ 0 0 0 1 0 0 0 kDUEcqnK0MrjH9hcYHDKUw 1 0 1 0 0 0 0 806PYZnmNaw4h0wh8fyWDQ 1 0 1 0 0 0 0 bvu13GyOUwhEjPum2xjiqQ 0 1 1 1 1 0 0 tvCT2yT3bLlpU2crUt0zJw 1 0 0 1 0 0 0 806

我有一个以这种格式保存的txt文件:

806PYZnmNaw4h0wh8fyWDQ 0 0 0 0 0 1 0 
bvu13GyOUwhEjPum2xjiqQ 0 0 0 1 0 0 0 
kDUEcqnK0MrjH9hcYHDKUw 1 0 1 0 0 0 0 
806PYZnmNaw4h0wh8fyWDQ 1 0 1 0 0 0 0 
bvu13GyOUwhEjPum2xjiqQ 0 1 1 1 1 0 0 
tvCT2yT3bLlpU2crUt0zJw 1 0 0 1 0 0 0 
806PYZnmNaw4h0wh8fyWDQ 1 1 1 0 0 0 0 
9Ify25DK87s5_u2EhK0_Rg 0 0 0 1 0 0 0 
bvu13GyOUwhEjPum2xjiqQ 0 1 0 1 0 0 0 
806PYZnmNaw4h0wh8fyWDQ 1 0 0 0 0 0 0 
zk0SnIEa8ju2iK0mW8ccRQ 0 0 0 0 1 0 0 
AMuyuG7KFWJ7RcbT-eLWrQ 0 0 0 1 0 0 0
现在我需要循环这个输入文件,并根据第一个编码值保存一个排序和重新排列的输出文件,该值可能在输入文件中出现多次?例如,在这个示例文件中,我在随机位置多次使用了
806PYZnmNaw4h0wh8fyWDQ

到目前为止,我试过: 私有静态最终字符串Path=“newuserfeature.txt”

publicstaticvoidmain(字符串[]args){
列表=新的ArrayList();
List uniquelist=new ArrayList();
试一试{
Scanner unique=新扫描仪(新文件读取器(路径));
while(unique.hasNextLine()){
字符串userUnique=unique.nextLine();
uniquelist.add(userUnique);
}
迭代器it=uniquelist.Iterator();
while(it.hasNext()){
字符串user=it.next();
int a=0;
int b=0;
int c=0;
int d=0;
int e=0;
int f=0;
int g=0;
迭代器it2=list.Iterator();
while(it2.hasNext()){
字符串[]y=it2.next().toString().split(“”);
if(user.equalsIgnoreCase(y[0])){
int a1=整数.parseInt(y[1]);
intb1=Integer.parseInt(y[2]);
int c1=整数.parseInt(y[3]);
intd1=Integer.parseInt(y[4]);
int e1=整数.parseInt(y[5]);
int f1=Integer.parseInt(y[6]);
intg1=Integer.parseInt(y[7]);
如果(a1==1){
a=a1;
}
如果(b1==1){
b=b1;
}
如果(c1==1){
c=c1;
}
如果(d1==1){
d=d1;
}
如果(e1==1){
e=e1;
}
如果(f1==1){
f=f1;
}
}
}
System.out.println(用户+“”+a+“”+b+“”+c+“”+d
+“+e+”+f+“+g+”\n”);
}
}catch(filenotfounde异常){
e、 printStackTrace();
}
}

如果您仅按第一个字段(碰巧是字符串)对行进行排序,您可以执行以下操作:

Path oldFile = Paths.get("/path/to/my/file");
Path newFile = Paths.get("/path/to/my/newFile");

List<String> lines = Files.readAllLines(oldFile);
Collections.sort(lines);
Files.write(newFile, lines);
Path oldFile=Path.get(“/Path/to/my/file”);
Path newFile=Path.get(“/Path/to/my/newFile”);
列表行=文件。readAllLines(旧文件);
集合。排序(行);
文件。写入(新文件,行);
其中:

1:加载字符串列表中的所有行,
2:按字符串排序(这是您在一天结束时想要的)
3:将行存储在另一个文件中


当您按照第一个字段进行字符串排序时,您不需要将每一行分解为它的列。

我尝试使用两个列表并根据第一个元素相互比较?您所说的“重新排列”是什么意思?您正在进行“字符串”排序吗?是的,根据输入文件中的第一个元素值进行排序。发布您尝试过的代码。请发布您想要的示例输出?
Path oldFile = Paths.get("/path/to/my/file");
Path newFile = Paths.get("/path/to/my/newFile");

List<String> lines = Files.readAllLines(oldFile);
Collections.sort(lines);
Files.write(newFile, lines);