Looking for opinions/advice. Java I/O time stamped log entry class


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 9 of 9

Thread: Looking for opinions/advice. Java I/O time stamped log entry class

Hybrid View

  1. #1
    Join Date
    Aug 2006
    Posts
    6

    Looking for opinions/advice. Java I/O time stamped log entry class

    I am currently having difficulties with an assignment, the assignment is to write a EntryLog class. The class diagram is as follows:
    _____________________________________________________
    LogEntry
    --------------------------------------------------------------------------------------------
    - dateStamp: DateTime //since that is deprecated, we can sub.
    - filename: String
    - message: String
    ---------------------------------------------------------------------------------------------
    + LogEntry(DateTime, String, String)
    + getDateStamp(): String
    + getFileName(): String
    + getMessage(): String
    + writeEntry(): void
    ---------------------------------------------------------------------------------------------

    My instructor prefers that we use PrintWriter, all entries must be time stamped with the current date and time, and can not be changed for security reasons. We must test our class(not with JUnit, but with something along the lines of a println) and verify that it works correctly. That is where I seem to be having one of my problems. If I call my default constructor, nothing happens(nothing is wrote). If I call my custom constructor, the date and time don't print to the output file. Can someone please point out where I am going wrong, Google has too many different examples, by too many different programmers, and just a snippet of code here and there relating to what I am looking for. Here is my source for the LogEntry class:

    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.Calendar;
    import java.util.Date;

    import javax.swing.JOptionPane;
    /*
    * author: joe marinaccio
    * date: 08/23/06
    * file: LogEntry.java
    * desc.: a class to write log entries to a text file,
    * and that are time stamped.
    */
    public class LogEntry{
    private Date dateStamp = null;
    private String filename = null;
    private String message = null;

    public LogEntry(){
    //default
    }

    public LogEntry(Date dateStamp, String filename, String message){
    this.dateStamp = dateStamp;
    this.filename = filename;
    this.message = message;
    this.writeEntry();
    }
    public void writeEntry(){

    try {
    boolean exists = (new File("LogEntry.log")).exists();
    if (exists) {
    try {
    boolean append = true;
    PrintWriter pw = new PrintWriter(new FileWriter("LogEntry.log", append));
    pw.print(dateStamp + " " + filename + " " + message + "\n");
    pw.close();
    } catch (IOException e) {
    JOptionPane.showMessageDialog(null, "error writing to log file\n" + e);
    }
    } else {
    try {
    @SuppressWarnings("unused")
    File file = new File("LogEntry.log");
    boolean append = true;
    PrintWriter pw = new PrintWriter(new FileWriter("LogEntry.log", append));
    pw.print(dateStamp + " " + filename + " " + message + "\n");
    pw.close();
    } catch (IOException e) {
    JOptionPane.showMessageDialog(null, "unable to create log file\n" + e);
    }
    }

    } catch (Exception e) {
    JOptionPane.showMessageDialog(null, "an error has occurred.\n" + e);
    }
    }
    //accessors
    public String getMessage(){
    return message;
    }

    public String getFileName(){
    return filename;
    }

    public Date getDateStamp(){
    dateStamp = Calendar.getInstance().getTime();
    return dateStamp;
    }
    }


    Here is how I am trying to test it:

    public class TestLogEntry {

    public static void main(String[] args) {

    LogEntry test1 = new LogEntry();
    }

    }

    Any advice would be much appreciated, thanks in advance for your time.
    I searched Google and read the manual... It told me to ask you guys!

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    Does it make any difference if you call dateStamp.toString() for your first entry in the pw.print() statement?

  3. #3
    Join Date
    Aug 2006
    Posts
    6
    Do you mean something like this?

    try {
    boolean append = true;
    PrintWriter pw = new PrintWriter(new FileWriter("LogEntry.log", append));
    pw.print(dateStamp.toString() + " " + filename + " " + message + "\n");
    pw.close();
    } catch (IOException e) {
    JOptionPane.showMessageDialog(null, "error writing to log file\n" + e);
    }

    That didn't seem to affect anything, still not outputting to the log file.
    I searched Google and read the manual... It told me to ask you guys!

  4. #4
    Join Date
    Dec 2005
    Posts
    97
    you're not calling the writeEntry method

    test1.writeEntry();

    that will create the file, but it will only write "null" three times. see if you can figure out why. if not i'll try to help you out.

  5. #5
    Join Date
    Aug 2006
    Posts
    6
    I had it completed Thursday, working great! But... when I went to turn it in, I noticed that I was passing the args to the writeEntry(). The class diagram provided, states that I need to pass the args to LogEntry instead. So... I started to move things around to meet the requirements, and now I am here. I totally understood it when I was passing the args to the writeEntry(), but now I just seem to be kind of lost on where I need to go. I have been trying different ideas out, one of them, I call my custom constructor, and pass it a date object(the one in getDateStamp()) and two Strings. It works fine, but if I just call the default constructor like so:
    LogEnty test1 = new LogEntry();
    nothing happens. If I do the same with:
    {
    test1.getDateStamp();
    test1.getMessage();
    test1.getFileName();
    test1.writeEntry();
    }
    then it seems to work fine. Is that not the correct way to instantiate the object? I am kind of unclear on alot of the syntax and usage of the language. I have been studying alot, and experimenting with my findings by writing code, as well as reviewing others code and trying to follow their logic.

    I appreciate all that have replied to this post, and all that will reply. If someone could just point me in the right direction, I would be very thankful.
    I searched Google and read the manual... It told me to ask you guys!

  6. #6
    Join Date
    Aug 2006
    Posts
    6
    anubis,

    Thank you. I posted, then looked at your reply again, then it clicked. I appreciate your help very much.

    Thanks again,
    marinaccio
    I searched Google and read the manual... It told me to ask you guys!

  7. #7
    Join Date
    Aug 2006
    Posts
    6
    Here is what I have come up with:
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.Calendar;
    import java.util.Date;

    import javax.swing.JOptionPane;
    /*
    * author: joe marinaccio
    * date: 08/23/06
    * file: LogEntry.java
    * desc.: a class to write log entries to a text file,
    * and that are time stamped.
    */
    public class LogEntry{
    private Date dateStamp;
    private String filename;
    private String message;

    public LogEntry(){
    //default
    }

    public LogEntry(Date dateStamp, String filename, String message){
    this.dateStamp = dateStamp;
    this.filename = filename;
    this.message = message;

    }
    public void writeEntry(){

    try {
    boolean exists = (new File("LogEntry.log")).exists();
    if (exists) {
    try {
    boolean append = true;
    PrintWriter pw = new PrintWriter(new FileWriter("LogEntry.log", append));
    pw.print(dateStamp + " " + filename + " " + message + "\n");
    pw.close();
    } catch (IOException e) {
    JOptionPane.showMessageDialog(null, "error writing to log file\n" + e);
    }
    } else {
    try {
    @SuppressWarnings("unused")
    File file = new File("LogEntry.log");
    boolean append = true;
    PrintWriter pw = new PrintWriter(new FileWriter("LogEntry.log", append));
    pw.print(dateStamp + " " + filename + " " + message + "\n");
    pw.close();
    } catch (IOException e) {
    JOptionPane.showMessageDialog(null, "unable to create log file\n" + e);
    }
    }

    } catch (Exception e) {
    JOptionPane.showMessageDialog(null, "an error has occurred.\n" + e);
    }
    }
    //accessors
    public String getMessage(){
    return message;
    }

    public String getFileName(){
    return filename;
    }

    public Date getDateStamp(){
    dateStamp = Calendar.getInstance().getTime();
    return dateStamp;
    }
    }

    and to test it, here is what I have:
    import java.util.Calendar;

    public class TestLogEntry {

    public static void main(String[] args) {
    //test1 - default constructor
    LogEntry test1 = new LogEntry();
    test1.getDateStamp();
    test1.getFileName();
    test1.getMessage();
    test1.writeEntry();

    //test2 - custom constructor
    LogEntry test2 = new LogEntry(Calendar.getInstance().getTime(), "/Some/FilePath/FileName", "Some message to log");
    test2.getDateStamp();
    test2.getFileName();
    test2.getMessage();
    test2.writeEntry();
    }

    }
    Does that seem about right? I think, so far. Thanks for all the advice.
    I searched Google and read the manual... It told me to ask you guys!

  8. #8
    Join Date
    Dec 2005
    Posts
    97
    looks good, except i dont see why you're calling these methods
    Code:
    // for test2 as well
    test1.getDateStamp();
    test1.getFileName();
    test1.getMessage();
    they arent doing anything helpful unless you are going to store them into variables or s.o.p them, ex:
    Code:
    System.out.println("Date: " + test2.getDateStamp() + "\nFile Name: " + test2.getFileName() + "\nMessage: " + test2.getMessage);
    try removing those lines and the program should work the same way. i think thats everything.

  9. #9
    Join Date
    Aug 2006
    Posts
    6
    In test2, I removed the additional lines. It still works great, thank you for the advice.
    I searched Google and read the manual... It told me to ask you guys!

Similar Threads

  1. JDOM Classpath Help Required
    By kpandya in forum Java
    Replies: 5
    Last Post: 01-15-2006, 07:10 PM
  2. OVERSEAS Online Data Entry Information
    By jai in forum Careers
    Replies: 0
    Last Post: 02-20-2003, 11:43 PM
  3. learning c# very confusing.
    By Mike Tsakiris in forum .NET
    Replies: 11
    Last Post: 10-04-2002, 05:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center