Java 从文本文件中读取一行并拆分其内容,对其进行排序,然后按照新的排序顺序将其放入文件中

Java 从文本文件中读取一行并拆分其内容,对其进行排序,然后按照新的排序顺序将其放入文件中,java,file,sorting,Java,File,Sorting,我是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:E

我是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:预期政策无政策,但报告“BPTHSRS-R05”发现12345 900000:预期政策无政策,但报告“AHQKEHB6-R05”发现12345 800005:预期政策无政策,但发现报告“AHQKEHB6-R05”为12345 700020:预期政策无政策,但为报告“AHQKEHB6-R05”找到12345 600000:预期政策无政策,但报告“AHQKEHB6-R05”发现12345


请给我举一个能帮助我的例子。提前谢谢。

以下是我的方法。这是文件IO中的一个简单练习,然后使用Collections.sort方法。正如他在评论中所提到的,如果要对整个字符串的前几个字符进行排序,则不需要拆分文本

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

简单的字母排序会给出这样的结果,无需过度设计拆分。这是正确的,但我只想指出,不建议在Java中使用Vector。请看下面的答案:@Tgsmith61591如果Vector是一个如此糟糕的东西,它将被弃用,但它不是,它是一个已知的遗留类。
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);
}