(1)本地读取资源文件
Java类中需要读取properties中的配置文件,可以采用文件(File)方式进行读取:注意 ../asset 因为在一个tomcat下 往上找一级就可以找到。
注意:当在IDEA中运行(不部署在服务器上),可以读取到该文件;
原因:JavaWeb项目部署服务器中,会将项目打包成Jar包或者war包,此时就不会存在 src/main/resources 目录,JVM会在编译项目时,主动将 java文件编译成 class文件 和 resources 下的静态文件放在 target/classes目录下;
理解:Java文件只有编译成 class文件才会被JVM执行,本地执行时会,当前项目即为Java进程的工作空间,虽然class文件在target/classes目录下,但是target/classes不是class文件运行的目录,只是存放的目录,运行目录还是在IDEA的模块下,所以运行时会找到 src/main/resources 资源文件!
(2)linux服务器(Tomcat)读取资源文件
在服务器上获取文件没来得及测试,后续测完之后再做更正。
public class I18nPropertiesReader {
private static final Logger logger = Logger.getLogger(I18nPropertiesReader.class.getClass());
private static Properties properties = new Properties();
private static final String resourceenEnName = "messages_en.properties";
private static final String resourcecnCnName = "messages_zh_CN.properties";
private static Properties prop_en = new Properties();
private static Properties prop_zh_cn = new Properties();
public static String configSrc = System.getProperty("user.dir").concat(File.separator);
static{
logger.info("加载i18N配置文件!");
try {
loadConfig();
logger.info("加载i18N配置文件完毕!");
} catch (Exception e) {
logger.error("加载配置文件失败:",e);
}
}
public static void loadConfig(){
try {
System.out.println(configSrc);
logger.info("项目路径:"+configSrc);
if(configSrc.endsWith("/")){
configSrc = configSrc.substring(0,configSrc.length()-1);
}
// 本地启动用这个加载方式
// File file = new File("../asset/src/main/resources/i18n/messages.properties");
// InputStream input = new FileInputStream(file);
// properties.load(input);
// input.close();
// File file1 = new File("../asset/src/main/resources/i18n/messages_en.properties");
// InputStream input1 = new FileInputStream(file1);
// prop_en.load(input1);
// input1.close();
// File file2 = new File("../asset/src/main/resources/i18n/messages_zh_cn.properties");
// InputStream input2 = new FileInputStream(file2);
// prop_zh_cn.load(new InputStreamReader(input2, "UTF-8"));
// input2.close();
// 跨项目 获取asset 资产项目下的 国际化属性。
String messages = configSrc + File.separator +"webapps" + File.separator + "asset" +File.separator + "WEB-INF"
+ File.separator + "classes" + File.separator + "i18n" + File.separator + "messages.properties";
String messages_en = configSrc + File.separator +"webapps" + File.separator + "asset" +File.separator + "WEB-INF"
+ File.separator + "classes" + File.separator + "i18n" + File.separator + "messages_en.properties";
String messages_zh_CN = configSrc + File.separator +"webapps" + File.separator + "asset" +File.separator + "WEB-INF"
+ File.separator + "classes" + File.separator + "i18n" + File.separator + "messages_zh_CN.properties";
File file = new File(messages);
InputStream input = new FileInputStream(file);
properties.load(input);
input.close();
logger.info("properties:"+properties);
File file1 = new File(messages_en);
InputStream input1 = new FileInputStream(file1);
prop_en.load(input1);
input1.close();
logger.info("prop_en:"+prop_en);
File file2 = new File(messages_zh_CN);
InputStream input2 = new FileInputStream(file2);
prop_zh_cn.load(new InputStreamReader(input2, "UTF-8"));
input2.close();
logger.info("prop_zh_cn:"+prop_zh_cn);
} catch (IOException e) {
logger.error("加载配置文件失败:",e);
}
}
/**
* ,第二个参数不传 默认是中文
* @param key
* @param languagetype
* @return
*/
public static String getValueByKey(String key,String languagetype){
if(languagetype!=null && "en".equals(languagetype)) {
return prop_en.getProperty(key);
}else {
return prop_zh_cn.getProperty(key);
}
}
}