从txt文件中读取单词并对其进行排序

时间:2014-10-29 08:26:40

标签: java sorting

我正在尝试读取test.txt并按顺序按字母顺序排序并将其打印到新文件sort.txt。 Test.txt在每行exp中只有1个单词。 :

  • 小鼠
  • 名词
  • SWAT

不能弄清楚我错过了什么。

管理找出我错过的东西!它写入文件,但它首先给我打了10000个空行,然后开始输出corectly。

现在到第二部分。

我的第二个问题与代码无关:我必须按长度和语言排序。我首先想的是按照长度排序他们。你觉得那会有用吗?有任何想法吗?完成后,所有单词都需要在txt文件中。

 package test;

   import java.io.BufferedReader;
   import java.io.FileReader;
   import java.io.FileWriter;
   import java.io.PrintWriter;
   import java.util.ArrayList;
   import java.util.Collections;
   import java.util.List;

    public class sort {

    public static void main(String[] args) throws Exception {

    String inputFile = "test.txt";
    String outputFile = "sort.txt";

    FileReader fileReader = new FileReader(inputFile);
    BufferedReader bufferedReader = new BufferedReader(fileReader);
    String inputLine;
    List<String> lineList = new ArrayList<String>();
    while ((inputLine = bufferedReader.readLine()) != null) {
        lineList.add(inputLine);
    }
    fileReader.close();

    Collections.sort(lineList);

    FileWriter fileWriter = new FileWriter(outputFile);
    PrintWriter out = new PrintWriter(fileWriter);
    for (String outputLine : lineList) {
        out.println(outputLine);
    }
    out.flush();
    out.close();
    fileWriter.close();

          }
     }

3 个答案:

答案 0 :(得分:1)

在Linux中,您可以从命令实用程序执行cat test.txt | sort > test1.txt。它非常简单。输出将存储在test1.txt

在java中你也可以使用字符串数组的sort方法,如下所示:

首先将文件读取到字符串数组 ///

        Path filePath = new File("fileName").toPath();
        Charset charset = Charset.defaultCharset();        
        List<String> stringList = Files.readAllLines(filePath, charset);
        String[] strNames = stringList.toArray(new String[]{});

然后对字符串数组strNames使用下面的方法,sort方法对数组进行排序

            //String array
            String[] strNames = new String[]{"John", "alex", "Chris", "williams", "Mark", "Bob"};

            /*
             * To sort String array in java, use Arrays.sort method.
             * Sort method is a static method.               *
             */

            //sort String array using sort method
            Arrays.sort(strNames);

            System.out.println("String array sorted (case sensitive)");

            //print sorted elements
            for(int i=0; i < strNames.length; i++){
                    System.out.println(strNames[i]);
            }

            /*
             * Please note that, by default Arrays.sort method sorts the Strings
             * in case sensitive manner.
             *
             * To sort an array of Strings irrespective of case, use
             * Arrays.sort(String[] strArray, String.CASE_INSENSITIVE_ORDER) method instead.
             */

答案 1 :(得分:0)

实施比较器并将其传递给Collections.sort(list,comparator)

在您的情况下,使用Collections.sort(lineList, new MyComparator());
代替Collections.sort(lineList);

class MyComparator implements Comparator<String> {
    public int compare(String s1, String s2) {
        if (s1.length() > s2.length()) {
            return 1;
        } else if (s1.length() < s2.length()) {
            return -1;
        } else {
            return s1.compareTo(s2);
        }
    }
}

比较函数,它对某些对象集合施加总排序。可以将比较器传递给排序方法(例如Collections.sort或Arrays.sort),以便精确控制排序顺序。比较器还可用于控制某些数据结构的顺序(例如有序集或有序映射),或者为不具有自然顺序的对象集合提供排序。
java docs

了解详情

答案 2 :(得分:0)

管理以使其有效。它首先按字母顺序排序,然后按长度排序。感谢您的所有回复。

package test;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class sort {
public static class MyComparator implements Comparator<String>{
    @Override
    public int compare(String o1, String o2) {  
      if (o1.length() > o2.length()) {
         return 1;
      } else if (o1.length() < o2.length()) {
         return -1;
      }
      return o1.compareTo(o2);
    }
}
public static void main(String[] args) throws Exception {

    String inputFile = "test.txt";
    String outputFile = "sort.txt";

    FileReader fileReader = new FileReader(inputFile);
    BufferedReader bufferedReader = new BufferedReader(fileReader);
    String inputLine;
    List<String> lineList = new ArrayList<String>();
    while ((inputLine = bufferedReader.readLine()) != null) {
        lineList.add(inputLine);
    }
    fileReader.close();

    Collections.sort(lineList,String.CASE_INSENSITIVE_ORDER);

    FileWriter fileWriter = new FileWriter(outputFile);
    PrintWriter out = new PrintWriter(fileWriter);
    for (String outputLine : lineList) {
        out.println(outputLine);
    }   

    Collections.sort(lineList, new MyComparator());

    FileWriter Fw = new FileWriter(outputFile);
    PrintWriter pw = new PrintWriter(fileWriter);
    for (String outputLine : lineList) {
        out.println(outputLine);
    }       
    out.flush();
    out.close();
    fileWriter.close();

}
}