JAVA按升序对新文件中的行进行排序并显示它

JAVA按升序对新文件中的行进行排序并显示它,java,eclipse,sorting,printwriter,Java,Eclipse,Sorting,Printwriter,我正在使用eclipse用java编写一个简单的猜谜游戏。现在我想按升序打印详细信息。我尝试了很多在这个论坛上找到的方法,但都没有解决。这就是为什么我需要你的帮助 我的代码如下: File f = new File ("scores.txt"); // name of the folder created at the root of the project String ligne; try { PrintWriter pw = new PrintWri

我正在使用eclipse用java编写一个简单的猜谜游戏。现在我想按升序打印详细信息。我尝试了很多在这个论坛上找到的方法,但都没有解决。这就是为什么我需要你的帮助

我的代码如下:

File f = new File ("scores.txt");
    // name of the folder created at the root of the project
    String ligne;
    try
    {
    PrintWriter pw = new PrintWriter (new BufferedWriter (new FileWriter (f, true)));
    if (numberOfTries==1){
        pw.println (numberOfTries + " try by " + str )  ; 
    }
    else if (numberOfTries!=1){
        pw.println (numberOfTries + " tries by " + str )    ; 
    }

//  pw.println (numberOfTries)  ;
    pw.close ();    
    // this line MUST be written.
    }

    catch (IOException exception) {
    System.out.println ("Error while writing data:" + exception.getMessage());
    }       

    BufferedReader ficTexte;
    // to read from a folder text.
try {
        ficTexte = new BufferedReader (new FileReader(f));
        do {
            ligne= ficTexte.readLine();
            if (ligne !=null)
            // if the line is not empty.
               System.out.println(ligne);
        } while (ligne !=null);
        ficTexte.close();
        System.out.println("\n");
        }
    // Show message in case of errors.
        catch (FileNotFoundException e) {
            System.out.println (e.getMessage());
        } 
        catch (IOException e) {
            System.out.println (e.getMessage());
        }

}
2 tries by NoNo
3 tries by Vibe
7 tries by Mata
10 tries by Caroline
10 tries by Lionel
如果我有

3 tries by Vibe
2 tries by NoNo
10 tries by Caroline
7 tries by Mata
10 tries by Lionel
我希望安排如下:

File f = new File ("scores.txt");
    // name of the folder created at the root of the project
    String ligne;
    try
    {
    PrintWriter pw = new PrintWriter (new BufferedWriter (new FileWriter (f, true)));
    if (numberOfTries==1){
        pw.println (numberOfTries + " try by " + str )  ; 
    }
    else if (numberOfTries!=1){
        pw.println (numberOfTries + " tries by " + str )    ; 
    }

//  pw.println (numberOfTries)  ;
    pw.close ();    
    // this line MUST be written.
    }

    catch (IOException exception) {
    System.out.println ("Error while writing data:" + exception.getMessage());
    }       

    BufferedReader ficTexte;
    // to read from a folder text.
try {
        ficTexte = new BufferedReader (new FileReader(f));
        do {
            ligne= ficTexte.readLine();
            if (ligne !=null)
            // if the line is not empty.
               System.out.println(ligne);
        } while (ligne !=null);
        ficTexte.close();
        System.out.println("\n");
        }
    // Show message in case of errors.
        catch (FileNotFoundException e) {
            System.out.println (e.getMessage());
        } 
        catch (IOException e) {
            System.out.println (e.getMessage());
        }

}
2 tries by NoNo
3 tries by Vibe
7 tries by Mata
10 tries by Caroline
10 tries by Lionel
怎么做呢?

试试这个:

private static final Comparator<String> CMP = new Comparator<String>()
{
    @Override
    public int compare(final String a, final String b)
    {
        final int inta = Integer.parseInt(a.split("\\s+")[0]);
        final int intb = Integer.parseInt(b.split("\\s+")[0]);
        return Integer.compare(inta, intb);
    }
}
专用静态最终比较器CMP=新比较器()
{
@凌驾
公共整数比较(最终字符串a、最终字符串b)
{
final int inta=Integer.parseInt(a.split(“\\s+”)[0]);
final int intb=Integer.parseInt(b.split(“\\s+”)[0]);
返回整数。比较(inta、intb);
}
}
然后,在后面的代码中:

final Path file = Paths.get("scores.txt");
final List<String> lines = Files.readAllLines(file, StandardCharsets.UTF_8);
Collections.sort(lines, CMP);

for (final String line: lines)
    System.out.println(line);
final Path file=Path.get(“scores.txt”);
最终列表行=Files.readAllLines(文件,StandardCharsets.UTF_8);
集合。排序(行,CMP);
用于(最后一行字符串:行)
系统输出打印项次(行);
(同样,您应该使用
文件.newBufferedWriter()
编写器
添加到您的文件中)

基本解决方案:

  • 编写代码读取文件中的所有行,并将它们存储在您选择的列表中(ArrayList似乎合理)。退房
  • 编写一个自定义比较器来执行排序。退房与fge答案一样,比较两个字符串。与这个答案不同的是,不要使用matcher,这对我来说太过分了。相反,只需调用String.split()并引用返回数组中的第一个元素(索引零)。将其转换为数字并执行比较
  • 排序(行,你的比较器)(就像在fge答案中一样)
  • 将行写入您选择的文件

  • 谢谢你的回复,但是我应该把私人密码放在哪里呢?我尝试在同一个java文件中插入,它给出了语法错误。我应该创建一个新的java文件并用私有函数执行java文件吗?不,只需在类的开头声明它(我想是说
    CMP
    )我在代码中输入了我的完整代码,我已经插入了“final Path…”请帮我纠正错误。这不是我提供的代码,你对CMP做了什么?它是一个
    比较器
    并在我编写时初始化,因为当我插入代码时,它会给出一个错误(CMP无法解析为变量),并建议添加一个静态变量,所以我继续。我该怎么办?我编辑了代码并删除了私有静态,它现在不起作用了。在CMP时出错。谢谢,但问题是我一直在尝试,并且仍然能够正确地进行操作。出现错误,但不确定在哪里。