在 Java 中对文本文件进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15649567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 20:25:15  来源:igfitidea点击:

Sorting a text file in Java

javafilesortingtext

提问by user2205055

I have a text file with a list of words which I need to sort in alphabetical order using Java. The words are located on seperate lines.

我有一个包含单词列表的文本文件,我需要使用 Java 按字母顺序对这些单词进行排序。这些词位于单独的行上。

How would I go about this, Read them into an array list and then sort that??

我将如何解决这个问题,将它们读入一个数组列表,然后对其进行排序??

回答by Jordan Dea-Mattson

This is a simple four step process, with three of the four steps addressed by Stackoverflow Questions:

这是一个简单的四步过程,Stackoverflow Questions 解决了四个步骤中的三个:

  1. Read each line and turn them into Java String
  2. Store each Java String in a Array (don't think you need a reference for this one.)
  3. Sort your Array
  4. Write out each Java String in your array
  1. 读取每一行并将它们转换为 Java String
  2. 将每个 Java 字符串存储在一个数组中(不要认为您需要对这个字符串的引用。)
  3. 对数组进行排序
  4. 写出数组中的每个 Java 字符串

回答by www.gadgetgyan.in

import java.io.*;
import java.util.*;

public class example
{
    TreeSet<String> tree=new TreeSet<String>();
    public static void main(String args[])
    {
        new example().go();
    }
    public void go()

    {
        getlist();
        System.out.println(tree);

    }
     void getlist()
    {
        try
        {
            File myfile= new File("C:/Users/Rajat/Desktop/me.txt");
            BufferedReader reader=new BufferedReader(new FileReader(myfile));
            String line=null;
            while((line=reader.readLine())!=null){
                addnames(line);


            }
        reader.close();
        }

        catch(Exception ex)
        {
            ex.printStackTrace();
        }

    }
    void addnames(String a)
    {
           tree.add(a);
           for(int i=1;i<=a.length();i++)
           {

           }
    }
}

回答by live-love

Here is an example using Collections sort:

这是使用集合排序的示例:

public static void sortFile() throws IOException
{     
    FileReader fileReader = new FileReader("C:\words.txt");
    BufferedReader bufferedReader = new BufferedReader(fileReader);
    List<String> lines = new ArrayList<String>();
    String line = null;
    while ((line = bufferedReader.readLine()) != null) {
        lines.add(line);
    }
    bufferedReader.close();

    Collections.sort(lines, Collator.getInstance());

    FileWriter writer = new FileWriter("C:\wordsnew.txt"); 
    for(String str: lines) {
      writer.write(str + "\r\n");
    }
    writer.close();
}

You can also use your own collation like this:

您还可以像这样使用自己的排序规则:

Locale lithuanian = new Locale("lt_LT");
Collator lithuanianCollator = Collator.getInstance(lithuanian);