Hello.

I've written a simple program that will once read text into a file and then append some other text when it starts reading again. In other words, adding text to an already existing one. My program doesn't do that but instead it keeps overwriting the older text. How can I change it so it will keep older text but append new ones in new line.

Here is my code:

                Scanner i = new Scanner(System.in);
                Scanner d = new Scanner(System.in);
                System.out.println("Updating information.....");
                System.out.println("\nUsername: ");
                String name = i.nextLine();
                System.out.println("\nAppointment Day: ");
                String day = i.nextLine();
                System.out.println("\nStarting Time: ");
                double start = d.nextDouble();
                System.out.println("\nEnding Time: ");
                double end = d.nextDouble();
                System.out.println("\nNotes/Comments: ");
                String note = i.nextLine();


                File file = new File("C://Users/s11065250/Assig1-Client/src/Sample.txt");

                if (!file.exists()) {
                    file.createNewFile();
                }
                FileWriter fw = new FileWriter(file.getAbsoluteFile());
                BufferedWriter bw = new BufferedWriter(fw);
                bw.append(name + " ");
                bw.append(day + " ");
                bw.append(String.valueOf(start) + " ");
                bw.append(String.valueOf(end) + " ");
                bw.append(note + " ");
                bw.newLine();
                bw.close();

                System.out.println("\nInformation updated successfully");

Another question, what is the appropriate data type to be used for TIME such as starting time and ending time. I used double but I think there must be a better option.

Thanks.

class FileWriter

-FileWriter(File file, boolean append)

Change line #21 (above) from:

FileWriter fw = new FileWriter(file.getAbsoluteFile());

To:

FileWriter fw = new FileWriter(file.getAbsoluteFile(),true);

it works, so I left with one more question on TIME appropriate data type, any suggestion what can I use instead of double. I know double would not be feasible if I enter 11:30.

There are a number of ways of storing Time. The java.util.Date object stores date and time. You can use it to just store time, but it's not ideal. In answer to developers' frustration with this and other issues around java.util.Date, new APIs were added to Java8.

The best fit for your use case is probably LocalTime.

Instead of reading the time in as a double, read it as a String.
You can then use DateTimeFormatter for parsing the time into a LocalTime object.

I'm always storing time as a String in text files, here's my static method for getting it:

public static String calcDate(long millisecs) {
     // long millisecs = System.currentTimeMillis();
     SimpleDateFormat date_format = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
     Date resultdate = new Date(millisecs);
     return date_format.format(resultdate);
} 

Hope this gives you an idea of how you can format the time to look like what you need?

Martin

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.