简体   繁体   中英

Sorting dates as Strings Java

I am trying to create a method that will take in user entered tasks and due dates and sort them by due date. I have the code written for the task and it compiles, but when I try to sort it seems to delete all the contents. Am I writing over the original file? How could I correct this to add it to the same file without deleting?

public class DueDate implements Comparable<DueDate>{

    public String addedTask = "";
    public String dueDate = "";


    public DueDate(String addedTask, String dueDate){

        this.addedTask = addedTask;
        this.dueDate = dueDate;
    }

    public String toString(){
        return addedTask+"\t"+dueDate+"\t";
    }

    @Override
    public int compareTo(DueDate o) {
        return this.dueDate.compareTo(o.dueDate);
    }
}

public class Main {

    public static String fileName = "/Users/eri/Desktop/tasklistjava/src/javatask.txt";

    public static void main(String[] args) throws IOException {

        int menuItem = -1;
        while(menuItem != 0){
            menuItem = menu();
            switch (menuItem){
                case 1:
                    showTaskList();
                    break;
                case 2:
                    addTask();
                    break;
                case 3:
                    sortList();
                case 4:
                    deleteTasks();
                    break;
                case 0:
                    break;
                default:
                    System.out.println("Invalid Input");

            }
        }
    }

static void sortList() throws IOException {
        System.out.println("\nSorted List\n");
        try {
            BufferedReader br = new BufferedReader(new FileReader(fileName));
            BufferedWriter bw = new BufferedWriter(new FileWriter(fileName));
            ArrayList<DueDate> tasks = new ArrayList<DueDate>();
            String line = "";
            while((line = br.readLine()) != null) {
                String[] values = line.split("-");

                if(values.length == 2) {
                    String addedTask = values[0];
                    String dueDate = values[1];

                    DueDate d = new DueDate(addedTask, dueDate);

                    tasks.add(d);
                }
            }

            Collections.sort(tasks);

            for(int i = 0; i < tasks.size(); i++){
                DueDate date = tasks.get(i);
                String lineText = date.toString();
                bw.write(lineText);
                bw.newLine();
            }

            br.close();
            bw.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

Usually when you want to sort records in a file, you'd first take the contents out and store it in an arraylist and sort it whilst it's in the arraylist. Afterwards you delete the old file and write the sorted data into a new file.

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