范例1:读取普通文本
import java.util.ResourceBundle;
public class Demo {
public static void main(String[] args) {
ResourceBundle rb = ResourceBundle.getBundle("Messages");
System.out.println(rb.getString("info"));
}
}
============分割线============
范例2:设置读取可变内容
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.ResourceBundle;
public class Demo {
public static void main(String[] args) {
ResourceBundle rb = ResourceBundle.getBundle("Messages");
String str = rb.getString("wel.msg");
System.out.println(MessageFormat.format(str, "斯巴达", new SimpleDateFormat("yyyy-MM-dd").format(new Date())));
}
}
============分割线============
范例3:读取中文文件
import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;
public class Demo {
public static void main(String[] args) {
Locale loc = new Locale("zh", "CN");
ResourceBundle br = ResourceBundle.getBundle("Messages", loc);
String str = br.getString("wel.msg");
String data = MessageFormat.format(str, "斯巴达");
System.out.println(data);
}
}
============分割线============
范例4:读取英文文件
import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;
public class Demo {
public static void main(String[] args) {
Locale loc = new Locale("en", "US");
ResourceBundle rb = ResourceBundle.getBundle("Messages", loc);
String str = rb.getString("wel.msg");
String data = MessageFormat.format(str, " James");
System.out.println(data);
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/51225ab8bbde4b6c5b26195b6b213c4f.png)
![](https://i-blog.csdnimg.cn/blog_migrate/c0bd3d3f35f854f528bf2d7c8b0c0812.png)