简体   繁体   English

如何在java中对多个文件中的数据进行排序

[英]How to sort data from multiple files in java

I am currently playing around with java and I am wondering if it is possible to read from a few files, and then sort the contents from all of them (alphabetically)?我目前正在玩 java,我想知道是否可以从几个文件中读取,然后对所有文件中的内容进行排序(按字母顺序)?

If that is possible would I be able to take an entire line, and sort it based on the first phrase?如果可能的话,我是否能够取整行,并根据第一个短语对其进行排序?

Example:例子:

This,这个,

ECE3111 A- 4.00 ECE3111 A- 4.00

ECE3031 A- 4.00 ECE3031 A- 4.00

CS1003 B+ 4.00 CS1003 B+ 4.00

Would return this,会返回这个,

CS1003 B+ 4.00 CS1003 B+ 4.00

ECE 3031 A- 4.00 ECE 3031 A- 4.00

ECE3111 A- 4.00 ECE3111 A- 4.00

The names will always have length 6 to 8 so is there a way of only sorting a line based on the first n number of places in the string?名称的长度总是 6 到 8,所以有没有办法只根据字符串中的前 n 个位置对一行进行排序?

Any tips would be greatly appreciated.任何提示将非常感谢。

Yes, it is possible.对的,这是可能的。 In order to do this, you would need to follow this general flow:为此,您需要遵循以下一般流程:

  • Create a list to store each line in创建一个list来存储每一行
  • Read first file (with a BufferedReader ), storing line by line into the list读取第一个文件(使用BufferedReader ),逐行存储到列表中
  • Repeat above for each file对每个文件重复上述操作
  • Sort list of lines (can be done natively using Java 's Collections library with Collections.sort(Collection<T> c)对行的排序列表(可以使用JavaCollections库和Collections.sort(Collection<T> c)本地完成
  • Output each line from files, or process it as you need输出文件中的每一行,或根据需要进行处理

First, create your list:首先,创建您的列表:

List<String> lines = new ArrayList<String>();

While we are reading the files, we will add each line to this list so we can store it, sort it, and output it later.在读取文件时,我们会将每一行添加到此list以便我们可以存储、排序和稍后输出。 Next, we need to read in each file and store it.接下来,我们需要读入每个文件并存储它。 A good guide on reading in files line by line in java can be found here .可以在此处找到有关在 Java 中逐行读取文件的良好指南。 I will do this with 1 file using a BufferedReader and the technique in the previous link.我将使用BufferedReader和上一个链接中的技术对 1 个文件执行此操作。 You can do this with multiple files by looping over the list of files that you need to read from.您可以通过遍历需要读取的文件列表来对多个文件执行此操作。

    BufferedReader fileReader = new BufferedReader(new FileReader("text.txt")); // Create reader with access to file
    String fileLine = fileReader.readLine(); // Read the first line
    while (fileLine != null) { // While there are still lines to read, keep reading
        lines.add(fileLine); // Store the current line
        fileLine = fileReader.readLine(); // Grab the next line
    }
    fileReader.close(); // Read all the lines, so close the read

The above code will read every line from the file text.txt and add it to the list that was created.上面的代码将从文件text.txt读取每一行并将其添加到创建的list中。 Now we can use the Java Collections library, and sort the list of lines using that.现在我们可以使用Java Collections库,并使用它对行list进行排序。 This can be done by calling Collections.sort(obj) .这可以通过调用Collections.sort(obj)来完成。 So, with our code, we call:因此,使用我们的代码,我们调用:

Collections.sort(lines);

Now, inside of our lines list variable, we have a sorted list of all of the lines in the file(s) that we read.现在,在我们的lines列表变量中,我们有一个我们读取的文件中所有行的排序list Now you can do whatever you need to with this sorted list !现在你可以用这个排序list做任何你需要做的事情! I decided to just output it.我决定只输出它。

    for(String line : lines){
        System.out.println(line);
    }

The full code is:完整代码是:

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


    List<String> lines = new ArrayList<String>();

    BufferedReader fileReader = new BufferedReader(new FileReader("text.txt")); // Create reader with access to file
    String fileLine = fileReader.readLine(); // Read the first line
    while (fileLine != null) { // While there are still lines to read, keep reading
        lines.add(fileLine); // Store the current line
        fileLine = fileReader.readLine(); // Grab the next line
    }
    fileReader.close(); // Read all the lines, so close the read

    Collections.sort(lines);
    for(String line : lines){
        System.out.println(line);
    }

}

I ran this code on the file text.txt which contained:我在包含以下内容的文件text.txt上运行此代码:

ECE3111 A- 4.00
ECE3031 A- 4.00
CS1003 B+ 4.00

And I got the output:我得到了输出:

CS1003 B+ 4.00
ECE3031 A- 4.00
ECE3111 A- 4.00

To get all files in a directory, we can create a list of String variables that represent a file's name.要获取目录中的所有文件,我们可以创建一个代表文件名的String变量list Then, loop over all files in a directory and check that they end with the extension that you are looking for.然后,遍历目录中的所有文件并检查它们是否以您要查找的扩展名结尾。

    List<String> textFileNames = new ArrayList<String>();
    File directory= new File("StackTxtFiles"); // Point to your directory you want to search
    for (File file : directory.listFiles()) // Loop over everything that exists in the directory
    {
       if (file.getName().endsWith(".txt")) // if the extension is ".txt", it's a text file so we should include it
       {
           textFileNames.add(file.getName()); // Add the file's name to the included list
       }
    }

Then, in your code for reading the files, put it inside of a loop looping over the textFileNames list and use the String variable as the file name passed into the FileReader rather than a hard-coded value.然后,在用于读取文件的代码中,将其放在循环textFileNames list循环中,并使用String变量作为传递给FileReader的文件名而不是硬编码值。

    List<String> lines = new ArrayList<String>();
    for(String fileName : textFileNames){ // Loop over each file that we found in the directory searching
        BufferedReader fileReader = new BufferedReader(new FileReader("StackTxtFiles/"+fileName)); // Create reader with access to file. Add the directory we are looking in
        String fileLine = fileReader.readLine(); // Read the first line
        while (fileLine != null) { // While there are still lines to read, keep reading
            lines.add(fileLine); // Store the current line
            fileLine = fileReader.readLine(); // Grab the next line
        }
        fileReader.close(); // Read all the lines, so close the read
    }

Yes it is possible.对的,这是可能的。

I would recommend loading all the Strings into an ArrayList<String> , call Collections.sort(<List name>);我建议将所有字符串加载到ArrayList<String> ,调用Collections.sort(<List name>); and then use an enhanced for loop to write it out to the file of your choosing.然后使用增强的for循环将其写出到您选择的文件中。

Hope this Helps!希望这可以帮助!

You can use a TreeSet with a comparator of your choice.您可以将 TreeSet 与您选择的比较器一起使用。

So Set A could have a comparator that compares the entire words.所以 Set A 可以有一个比较器来比较整个单词。

Set B could have a comparator that compares only the first 3 letters (substring) of the word. Set B 可以有一个比较器,只比较单词的前 3 个字母(子字符串)。

Then you just iterate through the files, add them to your sets, and you're done.然后您只需遍历文件,将它们添加到您的集合中,就完成了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM