1.在學習本章之前,需要有這樣幾個方法需要學習:
//的到的是使用者目前的工作目錄,參數是一個關鍵字,還有很多這樣的關鍵字,可以參見API
String path=System.getProperty("user.dir");
System.out.println(path);
現在自定義一個Log檔案,叫做log.txt,存放在工程裡面的目錄中。
public class Log {
private PrintWriter log = null; // 日志輸出流
private static Log logFactory = new Log(); // 建立唯一的執行個體,使用的是單例模式
/**
* 傳回唯一的執行個體
*/
public static Log getInstance() {
return logFactory;
}
/**
* 初始化日志檔案,同時 私有構造方法,防止外部對它進行執行個體化
*/
private Log() {
try {
/*
* 這樣得到的是class檔案的路徑,而不是目前類檔案的路徑,是以要獲得目前類檔案的路徑,這是錯誤的做法,
* 目前還沒有有效的方法獲得目前java檔案的路徑。是以棄之不用。 String
* logPath=Log.class.getResource("").toString().substring(6);
* 建立路徑和建立檔案要分開,但這裡得到的是已存在的路徑。
*/
String logPath = System.getProperty("user.dir");
File logFile = new File(logPath, "log.txt");
if (!logFile.exists()) {
logFile.createNewFile();
}
System.out.println(logFile.toString());
log = new PrintWriter(new FileOutputStream(logFile, true), true); // 建立日志輸出流
} catch (Exception e) {
log = new PrintWriter(System.err);
e.printStackTrace();
System.out.println("初始化日志檔案失敗");
}
}
/**
* 将異常寫入日志
*/
public void writeLog(Throwable e, String msg) {
// println方法是将内容列印在原先初始化使用的參數中,這裡當然就是file檔案了
log.println(new Date() + ":" + msg);
//這個方法是将異常列印到指定的輸出流中間,就是log.txt中
e.printStackTrace(log);
}
/**
* 将文本資訊寫入日志
*/
public void writeLog(String msg) {
log.println(new Date() + ":" + msg);
}
}
下面來看配置檔案是如何做到的:
配置檔案可以用來存一些常用的一些值,可以用來存儲一些不需要在資料庫中間存儲的值,操作起來相對友善。
大多數的配置檔案都是以.property或者xml檔案來結尾的,其實以什麼何時結尾并不重要,隻需是文本檔案類型即可。現在,來以ini結尾來建立一個屬性檔案。
第一步是建立一個ini檔案,注意在加載屬性之前,要確定這個配置檔案是存在的,如果不存在就需要建立。
public class UtilProperties {
private FileInputStream in; // 輸入流
private FileOutputStream out; // 輸出流
private Properties pro;
private Log log = Log.getInstance(); // 建立日志輸出對象
// 設定檔案的預設路徑,不需要外部來更改。
private String filePath = System.getProperty("user.dir") + "/property";
private String fileName=filePath+ "/config.ini";
public UtilProperties() {
getFile();
}
private File createFile(String fileName) throws IOException {
String logPath = fileName;
File propertiesFilePath = new File(logPath);
File propertiesFile = new File(logPath, "config.ini");
if (!propertiesFilePath.exists()) {
propertiesFilePath.mkdirs();
}
if (!propertiesFile.exists()) {
propertiesFile.createNewFile();
}
return propertiesFile;
}
private void getFile() {
try
{
File file = createFile(filePath);
in = new FileInputStream(file);
pro = new Properties();
pro.load(in); // 加載屬性檔案,加載以後,才可以對屬性檔案寫入值或者讀取值,加載的是輸入流,是讀取檔案
in.close();
} catch (FileNotFoundException e) {
log.writeLog(e,"配置檔案config.ini找不到!!");
} catch (IOException e) {
log.writeLog(e,"讀取配置檔案config.ini錯誤!!");
}
}
/*
* 列出所有配置檔案的内容,列印在控制台上面,具體參見API
*/
public void list() {
pro.list(System.out);
}
/*
* 指定配置項,傳回該配置項的值
*
*/
public String getItemValue(String key) {
return pro.getProperty(key);
}
/*
* 将配置儲存
*/
public void setItemValue(String key, String value) {
pro.setProperty(key, value);
}
/*
* 儲存配置檔案,指出檔案名及其路徑,這一步才是真的将檔案儲存在配置檔案中
*/
public void saveFile() {
try {
File file = new File(this.fileName);
out = new FileOutputStream(file);
pro.store(out, "");
out.close();
} catch (FileNotFoundException e) {
log.writeLog(e,"配置檔案config.ini找不到!!!");
} catch (IOException e) {
log.writeLog(e,"配置檔案config.ini寫入錯誤!!");
}
}
/*
* 删除一個屬性
*
*/
public void delete(String key) {
pro.remove(key);
}
/*
* 傳回配置檔案的路徑
*/
public String getFilePath() {
return this.fileName;
}
}
厘清哪些是私有方法,哪些是外部可以調用的方法。
測試,如何使用這些方法:
public class TestDemo {
public static void main(String[] args) {
UtilProperties utilProperties = new UtilProperties();
utilProperties.setItemValue("AAAA", "aaaa");
utilProperties.setItemValue("BBBB", "bbbb");
utilProperties.saveFile();
String a = utilProperties.getItemValue("AAAA");
System.out.println(a);
}
}