ResourceBundle 读取文件demo

ResourceBundle读取项目配置文件,
package object;

import java.text.MessageFormat;
import java.time.LocalDate;
import java.util.Locale;
import java.util.ResourceBundle;

/**
 * @author dayu 
 * @Describe 国际化程序,读取配置资源,配置文件后缀不需要填写,配置文件的位置一定要在classpath下, 如果有包名,需要添加包名,语言和地区可以在配置文件名称后面用下划线隔开
 */
public class ResourceBundleDemo {
    public static void main(String[] args) {

        Locale locale = Locale.getDefault();
        // Locale locale = new Locale("en", "US");
        ResourceBundle bundle = ResourceBundle.getBundle("object.Message", locale);
        // ResourceBundle bundle = ResourceBundle.getBundle("object.Message_zh");
        String content = bundle.getString("hello.say");
        // System.out.println(content);
        String format = MessageFormat.format(content, "tom", "lisi");
        System.out.println(format);
    }
}

配置文件demo的位置,一般配置在resources文件夹里面,则读取的时候不需要包名

猜你喜欢

转载自www.cnblogs.com/dayu007/p/10595336.html