简体   繁体   中英

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

I am beginner to java. I have a text file. I need to read the line from a text file and splitting its contents and sort them and place the line in the file with the new sorting order.

Lets say my file has the below contents:

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'

Here I need to sort the file in descending based on the first number (eg: 900000) and report.

So the result should be something like:

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

Please give me an example that helps me. Thanks in advance.

Here is how I would approach it. It is a simple exercise in file IO and then using the Collections.sort() method. As Marko Topolnik mentioned in his comment, you don't need to split the text if you are sorting on the first few chars in the whole String anyways.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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