簡體   English   中英

如何在java中對多個文件中的數據進行排序

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

我目前正在玩 java,我想知道是否可以從幾個文件中讀取,然后對所有文件中的內容進行排序(按字母順序)?

如果可能的話,我是否能夠取整行,並根據第一個短語對其進行排序?

例子:

這個,

ECE3111 A- 4.00

ECE3031 A- 4.00

CS1003 B+ 4.00

會返回這個,

CS1003 B+ 4.00

ECE 3031 A- 4.00

ECE3111 A- 4.00

名稱的長度總是 6 到 8,所以有沒有辦法只根據字符串中的前 n 個位置對一行進行排序?

任何提示將非常感謝。

對的,這是可能的。 為此,您需要遵循以下一般流程:

  • 創建一個list來存儲每一行
  • 讀取第一個文件(使用BufferedReader ),逐行存儲到列表中
  • 對每個文件重復上述操作
  • 對行的排序列表(可以使用JavaCollections庫和Collections.sort(Collection<T> c)本地完成
  • 輸出文件中的每一行,或根據需要進行處理

首先,創建您的列表:

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

在讀取文件時,我們會將每一行添加到此list以便我們可以存儲、排序和稍后輸出。 接下來,我們需要讀入每個文件並存儲它。 可以在此處找到有關在 Java 中逐行讀取文件的良好指南。 我將使用BufferedReader和上一個鏈接中的技術對 1 個文件執行此操作。 您可以通過遍歷需要讀取的文件列表來對多個文件執行此操作。

    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

上面的代碼將從文件text.txt讀取每一行並將其添加到創建的list中。 現在我們可以使用Java Collections庫,並使用它對行list進行排序。 這可以通過調用Collections.sort(obj)來完成。 因此,使用我們的代碼,我們調用:

Collections.sort(lines);

現在,在我們的lines列表變量中,我們有一個我們讀取的文件中所有行的排序list 現在你可以用這個排序list做任何你需要做的事情! 我決定只輸出它。

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

完整代碼是:

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);
    }

}

我在包含以下內容的文件text.txt上運行此代碼:

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

我得到了輸出:

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

要獲取目錄中的所有文件,我們可以創建一個代表文件名的String變量list 然后,遍歷目錄中的所有文件並檢查它們是否以您要查找的擴展名結尾。

    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
       }
    }

然后,在用於讀取文件的代碼中,將其放在循環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
    }

對的,這是可能的。

我建議將所有字符串加載到ArrayList<String> ,調用Collections.sort(<List name>); 然后使用增強的for循環將其寫出到您選擇的文件中。

希望這可以幫助!

您可以將 TreeSet 與您選擇的比較器一起使用。

所以 Set A 可以有一個比較器來比較整個單詞。

Set B 可以有一個比較器,只比較單詞的前 3 個字母(子字符串)。

然后您只需遍歷文件,將它們添加到您的集合中,就完成了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM