簡體   English   中英

從文本文件中讀取一行並將其內容拆分並排序,然后以新的排序順序將其放入文件中

[英]Reading a line from a text file and splitting its contents and sort them and place them in the file with the new sorting order

我是Java的初學者。 我有一個文本文件。 我需要從文本文件中讀取行並將其內容拆分並排序,然后將行以新的排序順序放入文件中。

可以說我的文件包含以下內容:

900000:Expected Policy# Nopolicy, but found 12345 for the report 'AHQKEHB6-R05'
700020:Expected Policy# Nopolicy, but found 12345 for the report 'AHQKEHB6- R05'
800005:Expected Policy# Nopolicy, but found 12345 for the report 'AHQKEHB6-R05'
900000:Expected Policy# Nopolicy, but found 12345 for the report 'BPTHSRS-R05'
600000:Expected Policy# Nopolicy, but found 12345 for the report 'AHQKEHB6-R05'

在這里,我需要根據第一個數字(例如:900000)以降序對文件進行排序並進行報告。

所以結果應該是這樣的:

900000:預期策略為Nopolicy,但為報告“ BPTHSRS-R05”找到12345
900000:預期策略為Nopolicy,但為報告“ AHQKEHB6-R05”找到12345
800005:預期的策略號為Nopolicy,但為報告“ AHQKEHB6-R05”找到了12345
700020:預期的策略號為Nopolicy,但為報告“ AHQKEHB6-R05”找到了12345
600000:預期策略為Nopolicy,但為報告“ AHQKEHB6-R05”找到12345

請給我一個例子,對我有幫助。 提前致謝。

這就是我的處理方法。 這是在文件IO中然后使用Collections.sort()方法的簡單練習。 正如Marko Topolnik在他的評論中提到的那樣,如果您要對整個String中的前幾個字符進行排序,則無需拆分文本。

public static void main(String[] args) {
    File myFile = new File("/path/to/file"); //WHEREVER YOUR FILE IS

    /* Read in file */
    BufferedReader reader = null;
    ArrayList<String> output = new ArrayList<String>();
    try {
        reader = new BufferedReader(new FileReader(myFile));
        String line = "";

        while((line = reader.readLine()) != null) {
            output.add(line);
        }
    } catch(IOException e) {
        e.printStackTrace();
    } finally {
        try {
            reader.close();
        } catch(IOException e) {
            e.printStackTrace();
        }
    }


    // Sort now
    Collections.sort(output, Collections.reverseOrder()); //Reverse order because your example asked for descending

    /* Now write */
    FileWriter writer = null;
    try {
        writer = new FileWriter("/path/to/sorted_file"); //Wherever you want it

        int idx = 0;
        for(String string : output) {
            String append = string + (++idx < output.size() ? System.getProperty("line.separator") : "");
            writer.write(append);
        }
    } catch(IOException e) {
        e.printStackTrace();
    } finally {
        try {
            writer.close();
        } catch(IOException e) {
            e.printStackTrace();
        }
    }
}
public String[] fileToString(String path) {
    File file = new File(path);
    Vector<String> contents = new Vector<String>();
    BufferedReader br = null;
    try {
        if (file.exists() && file.canRead()) {
            br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
            String line;
            while ((line = br.readLine()) != null) {
                contents.add(line);
            }
            br.close();
        }
    } catch (IOException e) {
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
            }
        }
    }
    return contents.toArray(new String[contents.size()]);
}

public void stringToFile(String path, String[] lines) {
    File file = new File(path);
    BufferedWriter bw = null;
    try {
        bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
        for (int i = 0; i < lines.length; i++) {
            bw.write(lines[i] + "\n");
        }
    } catch (IOException e) {
    } finally {
        if (bw != null) {
            try {
                bw.close();
            } catch (IOException e) {
            }
        }
    }
}

public void example() {
    String[] lines = fileToString("myFile.txt");
    Arrays.sort(lines, Collections.reverseOrder());
    stringToFile("myOtherFile.txt", lines);
}

暫無
暫無

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

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