1. 程式人生 > >java web專案建立讀取資原始檔的Messages類

java web專案建立讀取資原始檔的Messages類


package com.scheduler.util;

import java.util.MissingResourceException;
import java.util.ResourceBundle;

public class  Messages {
	//com.scheduler.messages是messages檔案的路徑,messages.properties存放常量資訊
	private static final String BUNDLE_NAME = "com.scheduler.messages";
	//ResourceBundle類是用來讀取資原始檔的類
	private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle
			.getBundle(BUNDLE_NAME);

	private Messages() {
	}

	public static String getString(String key) {
		try {
			return RESOURCE_BUNDLE.getString(key);
		} catch (MissingResourceException e) {
			return '!' + key + '!';
		}
	}
}