パクリ版のLog 4 jテスト入出力ストリーム第五講091205 B(補足)

5178 ワード

1)1行ずつ読み込む
2)次の文字filereaderを読みます.read()は、一度に1つ書きます.
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
パクリ版のLog 4 j
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私は間違っています.