从文本文件中读取一行并拆分其内容并对其进行排序,并使用新的排序顺序将它们放入文件中

时间:2015-02-27 21:12:24

标签: 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: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,但发现12345报告'BPTHSRS-R05'
900000:预期政策#Nopolicy,但发现12345报告'AHQKEHB6-R05'
800005:预期政策#Nopolicy,但发现12345报告'AHQKEHB6-R05'
700020:预期政策#Nopolicy,但发现12345报告'AHQKEHB6-R05'
600000:预期政策#Nopolicy,但报告'AHQKEHB6-R05'发现12345

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

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

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