Wednesday, 22 December 2010


University requires we keep some form of log book. Physical or virtual. As I am doing software a virtual one sounded good. Now, I'm not rich so I use Open Office. Compatibility and laziness meant I didn't want to use any word editting software like Microsoft Word or Open Writer. Using notepad is also pretty lame. Now I don't need to know all the crap I had written in earlier; don't need to correct spelling mistakes; CLI is fine; all I need is a time stamp with each post. Didn't want an online thing, like this blog, incase I couldn't get online when I needed it. So I decided to create my own note taking program.

tl;dr Need a program to take notes and timestamp.

Decided to go with Java for this one. Just because I was using it at the time. This would probably be much better had it been written in C or something equiv. Since there is no need for high performance the language speed is irrelevent. I went with a basic CLI interface that logs each 'post' with a timestamp on an enter key press. Having seen a friend make a similar GUI version and struggle with clicking a button for each post, he was trying to include multiline posts, it seemed the CLI was a good plan. The posts are only timestamped if the times differ by more than a minute. This will probably be increased later on as up to the minute is a bit over the top for recording information in this case.


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;
 * @author Michael Standen
 * Saves my notes, with appropriate time stamp.
public class MiNote {

  * @param args
  * @throws IOException
 public static void main(String[] args) throws IOException {
  // Initialise things.
  Scanner scanner = new Scanner(;
  System.out.println("Please enter the path of the notes file:");
  File notes = new File(scanner.nextLine());
  // Checks file exists.
  while (!notes.exists()){
   System.out.println("File does not exist. Create new? Y/N");
   if (scanner.nextLine().equalsIgnoreCase("y")){
    // Creates a new file with the name provided.
    if (!notes.createNewFile()){
     // Error in creation, get new file name.
     System.out.println("Error in file creation. Please enter new file path:");
     notes = new File(scanner.nextLine());
    } else {
     new BufferedWriter(new FileWriter(notes, true)).append("Created " +
       new SimpleDateFormat("(d MMM yyyy 'at' HH:mm z)").format(new Date())
       + '\n').close();
   } else {
    System.out.print("Please enter the path of the notes file:");
    notes = new File(scanner.nextLine());
  // Success. Outputs the current date.
  System.out.println("Success. The current time is: " +
    new SimpleDateFormat("(d MMM yyyy 'at' HH:mm z)" + '\n').format(new Date()));
  // Write the opening date and time.
  new BufferedWriter(new FileWriter(notes, true)).append("" + '\n' + "Opened " +
    new SimpleDateFormat("(d MMM yyyy 'at' HH:mm z)" +
      '\n').format(new Date())).close();

   String line = scanner.nextLine();
   if (line.equalsIgnoreCase("q")){
    // Quits the program.
   } else if (line.equalsIgnoreCase("r")){
    // Outputs the entire file.
   } else {
    writeNotes(notes, line);
  * Appends the note to the file and adds appropriate time and date stamp.
  * @param notes the file to be writen to.
  * @param line the String to be added.
  * @throws IOException
 private static void writeNotes(File notes, String line) throws IOException{
  // Make the file writable.
  // Create the file writer.
  BufferedWriter writer = new BufferedWriter(new FileWriter(notes, true));
  // Create date variables to see if time stamp is applicable.
  Calendar now = Calendar.getInstance();
  now.setTime(new Date());
  Calendar edit = Calendar.getInstance();
  edit.setTime(new Date(notes.lastModified()));
  // Create formatter for the time stamp
  SimpleDateFormat formatter;
  // Appends the date if it is different then the last edited date.
  if (now.get(Calendar.DAY_OF_MONTH) != edit.get(Calendar.DAY_OF_MONTH)){
   formatter = new SimpleDateFormat("(d MMM yyyy 'at' HH:mm z)");
   writer.append("" + '\n' + formatter.format(new Date()) + ": " + '\n');
  } else if (now.get(Calendar.HOUR_OF_DAY) != edit.get(Calendar.HOUR_OF_DAY) ||
    now.get(Calendar.MINUTE) != edit.get(Calendar.MINUTE)){
   formatter = new SimpleDateFormat("(HH:mm z)");
   writer.append("" + '\n' + formatter.format(new Date()) + ": " + '\n');
  writer.append(line + '\n');
  * Reads the file and prints the standard output.
  * @param notes the file to be read.
  * @return false if unable to read, true otherwise.
  * @throws FileNotFoundException
 private static void readNotes(File notes) throws FileNotFoundException {
  // Outputs the file.
  Scanner scanner = new Scanner(notes);

CLI programming is cool. If someone has no idea what's going on, they will be impressed. Also comments are important. Remember that.

No comments:

Post a Comment