class - Import Classes Into Java Files -
i learning develop in java , interested in creating java classes other users can import program , use. yes, know example class simple , stupid want learn concept , start making more complex classes people can import projects.
i created simple "logger" class when called logs both text console , text file readability. can call class using following commands...
logger logger = new logger(); logger.create(); logger.log("this text logged console , log.log");
see below logger class.
import java.io.bufferedwriter; import java.io.file; import java.io.filewriter; import java.text.simpledateformat; import java.util.calendar; import java.util.date; public class logger { filewriter fw; bufferedwriter br; file file = new file("log.log"); boolean fileexists = file.exists(); public void log(string message) { try { fw = new filewriter(file, true); br = new bufferedwriter(fw); calendar cal = calendar.getinstance(); int hour = cal.get(calendar.hour_of_day); if(hour > 12) hour = hour - 12; int minute = cal.get(calendar.minute); int second = cal.get(calendar.second); int millis = cal.get(calendar.millisecond); int ampm = cal.get(calendar.am_pm); string ampmstring; if(ampm == 1) ampmstring = "pm"; else ampmstring = "am"; string = string.format("%02d:%02d:%02d.%03d %s", hour, minute, second, millis, ampmstring); system.out.println(now + " - " + message); br.write(now + " - " + message); br.newline(); br.close(); } catch (exception err) { system.out.println("error"); } } public void create() { try { fw = new filewriter(file, true); br = new bufferedwriter(fw); simpledateformat sdf = new simpledateformat("mm-dd-yyyy"); string datestring = sdf.format(new date()); if(file.length() != 0) br.newline(); system.out.println("log: " + file.getabsolutepath()); br.write("--------------------" + datestring + "--------------------"); br.newline(); br.close(); } catch (exception err) { system.out.println("error"); } } }
the issue having in order use class have add every single project create , want use this. there way can add import like, mydomain.logger.*;
, able access class , methods contains?
my question, what best way allow import/use logger class in simplest way? steps need take allow them this?
compile class jar file, , add jar classpath of each project. standard java approach reusing classes.
Comments
Post a Comment