1.项目中的实际应用
这个demo用到了JSR75 - FileConnection Optional Package。直接看代码:
(1)File类
package cn.navi.util;
import java.io.IOException;
import java.io.OutputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
public class File {
public static void writeFile(String content) {
if(System.getProperty("microedition.io.file.FileConnection.version") != null ) { // file.separator
String url = "file:///root1/data.txt"; // sun DefaultColorPhone
// String url = "file://localhost/E:/data.txt"; // nokia phone
FileConnection fconn = null;
try {
fconn = (FileConnection) Connector.open(url, Connector.READ_WRITE);
if (!fconn.exists()) {
fconn.create();
if (!fconn.canWrite()) fconn.setWritable(true);
OutputStream out = fconn.openOutputStream();
out.write(content.trim().getBytes("utf-8"));
out.flush();
out.close();
} else {
if (!fconn.canWrite()) fconn.setWritable(true);
OutputStream out = fconn.openOutputStream();
out.write(content.trim().getBytes("utf-8"));
out.flush();
out.close();
}
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (fconn != null) {
try {
fconn.close();
} catch (IOException e) {
e.printStackTrace();
}
fconn = null;
}
}
}
}
}
(2)LogManager类
package cn.navi.util;
public final class LogManager {
private static Logger log = new LoggerImpl();
/**
* 安装自己实现的日志记录器/显示器
* @param log 新的日志记录器/显示器
*/
public static void install(Logger log) {
LogManager.log = log;
}
/**
* 记录OFF/FATAL/ERROR/WARN/INFO/DEBUG/TRACE/ALL级别的日志
* @param 日志内容
*/
public static void off(String off) {
log.log(Logger.OFF, off);
}
public static void fatal(String fatal) {
log.log(Logger.FATAL, fatal);
}
public static void error(String error) {
log.log(Logger.ERROR, error);
}
public static void warn(String warn) {
log.log(Logger.WARN, warn);
}
public static void info(String info) {
log.log(Logger.INFO, info);
}
public static void debug(String debug) {
log.log(Logger.DEBUG, debug);
}
public static void trace(String trace) {
log.log(Logger.TRACE, trace);
}
public static void all(String all) {
log.log(Logger.ALL, all);
}
public static void clearLog() {
log.clearLog();
}
public static String getLogContent() {
return log.getLogContent();
}
static class LoggerImpl implements Logger {
private StringBuffer sb;
public LoggerImpl() {
sb = new StringBuffer();
}
public void log(int level, String content) {
sb.append(getLevelName(level)).append(":").append(content).append("\n");
}
private String getLevelName(int level) {
switch (level) {
case OFF:
return "OFF";
case FATAL:
return "FATAL";
case ERROR:
return "ERROR";
case WARN:
return "WARN";
case INFO:
return "INFO";
case DEBUG:
return "DEBUG";
case TRACE:
return "TRACE";
case ALL:
return "ALL";
default:
return "UNKNOWN";
}
}
public String getLogContent() {
return sb.toString();
}
public void clearLog() {
sb.delete(0, sb.length());
}
}
}
interface Logger {
public static final int OFF = 70, // 最高等级的,用于关闭所有日志记录
FATAL = 60,
ERROR = 50,
WARN = 40,
INFO = 30,
DEBUG = 20,
TRACE = 10,
ALL = 0; // 最低等级的,用于打开所有日志记录
/**
* 实现的log方法
* @param level 级别
* @param info 内容
*/
public void log(int level, String content);
/**
* 得到所有的日志内容
* @return 内容
*/
public String getLogContent();
/**
* 清除当前的日志
*/
public void clearLog();
}
(3)使用方法
LogManager.debug("系统出现Debug");
LogManager.error("系统出现Error");
LogManager.fatal("系统出现Fatal");
LogManager.warn("系统出现Warn");
File.writeFile(LogManager.getLogContent());
2. 参考Demo
package forrest.logger;
/**
* 一个用于显示日志的接口,此接口用于LogManager来调用
* 日志显示器,因为日志记录了之后,肯定是要被我们显示出来的,想要如何显示,可以实现此接口.
* @author Forrest.He
*/
public interface LogShower {
/**
* 显示日志,由LogManager调用此方法来显示日志
* 显示日志可以有多种方法,比如可以用列表来显示
* 也可以用TextArea来显示,还可以用Canvas来显示
* @param logContent 日志内容
* @param action 返回的时候要做的动作
*/
public void showLog(String logContent, BackAction action);
/**
* 内部的一个静态接口,实现此接口以供LogShower在点击了返回之后,要做的事情
*/
public static interface BackAction {
/**
* 点击返回之后要做的事情
*/
public void back();
}
}
package forrest.logger;
/**
* 一个日志生成器要实现的接口, 提供了日志的记录器所要做的一些事情.
* @author forrest.he
*/
public interface Logger {
public static final int OFF = 70, // 最高等级的,用于关闭所有日志记录
FATAL = 60,
ERROR = 50,
WARN = 40,
INFO = 30,
DEBUG = 20,
TRACE = 10,
ALL = 0; // 最低等级的,用于打开所有日志记录
/**
* 实现的log方法
* @param level 级别
* @param info 内容
*/
public void log(int level, String content);
/**
* 得到所有的日志内容
* @return 内容
*/
public String getLogContent();
/**
* 清除当前的日志
*/
public void clearLog();
}
package forrest.logger;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import forrest.logger.LogShower.BackAction;
public final class LogManager extends MIDlet {
private static Logger log = new LoggerImpl();
private static LogShower shower = new LogShowerImpl();
protected static Display display;
//midlet的构造函数要被系统调用的,如果设为private在这一步就出错了。
//在运行midlet 的时候,实实上运行的是wtk的main函数,然后层层调用,调用了你了midlet的构造函数。以及startApp函数。
public LogManager() {
display = Display.getDisplay(this);
}
/**
* 安装自己实现的日志记录器/显示器
* @param log 新的日志记录器/显示器
*/
public static void install(Logger log) {
LogManager.log = log;
}
public static void install(LogShower shower) {
LogManager.shower = shower;
}
/**
* 记录OFF/FATAL/ERROR/WARN/INFO/DEBUG/TRACE/ALL级别的日志
* @param 日志内容
*/
public static void off(String off) {
log.log(Logger.OFF, off);
}
public static void fatal(String fatal) {
log.log(Logger.FATAL, fatal);
}
public static void error(String error) {
log.log(Logger.ERROR, error);
}
public static void warn(String warn) {
log.log(Logger.WARN, warn);
}
public static void info(String info) {
log.log(Logger.INFO, info);
}
public static void debug(String debug) {
log.log(Logger.DEBUG, debug);
}
public static void trace(String trace) {
log.log(Logger.TRACE, trace);
}
public static void all(String all) {
log.log(Logger.ALL, all);
}
/**
* 显示当前日志管理器的日志
* @param back 要返回的时候,做的动作
*/
public static void showLog(BackAction back) {
shower.showLog(log.getLogContent(), back);
}
/**
* 清除当前日志管理器的日志
*/
public static void clearLog() {
log.clearLog();
}
protected void startApp() throws MIDletStateChangeException {
off("off");
fatal("fatal致命错误");
error("error错误");
warn("warn警告");
info("info信息");
debug("debug");
trace("trace");
all("all");
LogManager.showLog(new BackAction() {
public void back() {
clearLog();
try {
destroyApp(true);
notifyDestroyed();
} catch (MIDletStateChangeException ex) {
ex.printStackTrace();
}
}
});
}
protected void pauseApp() {
}
protected void destroyApp(boolean unconditional) throws MIDletStateChangeException {
}
static class LogShowerImpl implements LogShower, CommandListener {
private BackAction action;
private Command backCommand, clearCommand;
private TextBox ta;
public void showLog(String logContent, final BackAction action) {
this.action = action;
backCommand = new Command("Back", Command.BACK, 1);
clearCommand = new Command("Clear", Command.CANCEL, 1);
ta = new TextBox("", logContent, 100, TextField.UNEDITABLE);
ta.addCommand(backCommand);
ta.addCommand(clearCommand);
ta.setCommandListener(this);
display.setCurrent(ta);
}
public void commandAction(Command c, Displayable d) {
if (c == backCommand) {
action.back();
} else if (c == clearCommand) {
clearLog();
ta.setString(null);
}
}
}
static class LoggerImpl implements Logger {
private StringBuffer sb;
public LoggerImpl() {
sb = new StringBuffer(1024);
}
public void log(int level, String content) {
// sb.append(getPrefix()).append("\n").append(getLevelName(level)).append(":").append(content).append("\n");
sb.append(getLevelName(level)).append(":").append(content).append("\n");
}
private String getPrefix() {
return "[" + Thread.currentThread() + "-" + System.currentTimeMillis() + "]";
}
private String getLevelName(int level) {
switch (level) {
case OFF:
return "OFF";
case FATAL:
return "FATAL";
case ERROR:
return "ERROR";
case WARN:
return "WARN";
case INFO:
return "INFO";
case DEBUG:
return "DEBUG";
case TRACE:
return "TRACE";
case ALL:
return "ALL";
default:
return "UNKNOWN";
}
}
public String getLogContent() {
return sb.toString();
}
public void clearLog() {
sb.delete(0, sb.length());
}
}
}