山寨版的Log4j 测试输入输出流 第五讲091205B(补充)

1)一次读入一行
2)读下一个字符filereader.read(),写一次读一个。
TestFileReader.java
测试输入输出流。

import java.io.*;
public class TestFileReader {
public static void main(String[] args) {

try {
Writer w = new FileWriter("e:\\5.txt");
w.write("信曾哥,");
w.write("考本科。");
w.close();
} catch (IOException e) {
e.printStackTrace();
}

try {
Writer w = new FileWriter("e:\\6.txt");
BufferedWriter bw2 = new BufferedWriter(w);
bw2.write("信春哥,");
bw2.write("得永生!");
bw2.flush();
bw2.close();
} catch (IOException e) {
e.printStackTrace();
}

try {
FileReader fr2 = new FileReader("e:\\5.txt");
BufferedReader br = new BufferedReader(fr2);
System.out.println(br.readLine());//(1)
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

try {
FileReader fr = new FileReader("e:\\6.txt");
int r;
while ((r = fr.read()) != -1) { //(2)
System.out.print((char) r);
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}

输出:
信曾哥,考本科。
信春哥,得永生!
------------------------------------------------------
Log4jDemo.java
山寨版的Log4j

MyLog4j.properties(此文件在工程文件夹下)

#info debug warn error
grate=debug
#file system
out=system

LogLevel.java

package test;
public interface LogLevel {
String ERROR="error";
String WARN="warn";
String DEBUG="debug";
String INFO="info";
String FILE="file";
String SYSTEM="system";
}

MyLog4j.java

package test;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

public class MyLog4j {
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private String grate = "";
private String output = "";
private FileWriter fw;
private BufferedWriter bw;

void init() throws FileNotFoundException, IOException {
Properties prop = new Properties();
prop.load(new FileInputStream("MyLog4j.properties"));
grate = prop.getProperty("grate");
output = prop.getProperty("out");
}

MyLog4j(String s) throws FileNotFoundException, IOException {

init();
if (LogLevel.FILE.equals(output)) {
fw = new FileWriter(s, true);// 第三个参数意义为累加
bw = new BufferedWriter(fw);
}
}

static MyLog4j getLogger(String s) throws FileNotFoundException,
IOException {
return new MyLog4j(s);
}

void info(String s) throws IOException {
if (LogLevel.DEBUG.equals(grate))
return;
if (LogLevel.ERROR.equals(grate))
return;
if (LogLevel.WARN.equals(grate))
return;
this.out(s);
}

void debug(String s) throws IOException {
if (LogLevel.ERROR.equals(grate))
return;
if (LogLevel.WARN.equals(grate))
return;
this.out(s);
}

void warn(String s) throws IOException {
if (LogLevel.ERROR.equals(grate))
return;
this.out(s);
}

void error(String s) throws IOException {
this.out(s);
}

void out(String s) throws IOException {
if (LogLevel.FILE.equals(output))
put1(s);
if (LogLevel.SYSTEM.equals(output))
put2(s);
}

void put1(String s) throws IOException {
this.bw.write(sdf.format(new Date())+" " +s);
this.bw.newLine();
this.bw.flush();// 记住要冲一下。。。。。。。。。。。
}

void put2(String s) {
System.out.println(sdf.format(new Date())+" " +s);
}
}

TestMyLog4J.java

package test;
import java.io.FileNotFoundException;
import java.io.IOException;
public class TestMyLog4J {
public static void main(String[] args) throws FileNotFoundException, IOException {
MyLog4j log = MyLog4j.getLogger("e:/1.log");
log.info("这只是个信息");
log.debug("我时在调试中。。。。。。");
log.warn("警告,注意。。。。。");
log.error("我错了。 ");
}
}


输出:
2009-12-16 22:36:46 我时在调试中。。。。。。
2009-12-16 22:36:46 警告,注意。。。。。
2009-12-16 22:36:46 我错了。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值