public static HashMap<Integer, String> loadLanguage() {
HashMap<Integer, String> languages = new HashMap<>();
HashMap<String, Integer> languaged = new HashMap<>();
int i = 0;
try {
FileInputStream fis = new FileInputStream(LanguageManger.class.getResource("/")+File.separator+"language"+File.separator+"zh_CN"+File.separator+"_LocaleText.csv");//这样写是获取的路径是class的文件
byte[] b = new byte[3];
fis.read(b);
String code = "GBK";
if (b[0] == -17 && b[1] == -69 && b[2] == -65) {
code = "UTF-8";
}
InputStreamReader isw = new InputStreamReader(fis, code);
try (BufferedReader br = new BufferedReader(isw)) {
boolean bReadNext = true;
while (bReadNext) {
i++;
// 一行
String strReadLine = br.readLine();
if (strReadLine == null) {
break;
}
String[] ss = strReadLine.split(",");
String str = ss[0];
int id = Integer.parseInt(str);
str = strReadLine.replaceFirst(str, "");
str = str.replaceFirst(",", "");
if (ss.length > 2) {
int length = str.length();
str = str.substring(1, length - 1);
}
languages.put(id, str);
languaged.put(str, id);
}
}
} catch (IOException | NumberFormatException e) {
log.error(e);
}
System.out.println(languaged.toString());
return languages;
}
问答
在启服时,在MainSetup模块中初始化加载语言模块下某一文件,怎么获取他的绝对路径?
标签:
mvc
6 回复
public static HashMap<Integer, String> loadLanguage() {
HashMap<Integer, String> languages = new HashMap<>();
HashMap<String, Integer> languaged = new HashMap<>();
int i = 0;
try {
FileInputStream fis = new FileInputStream(LanguageManger.class.getResource("/")+File.separator+"language"+File.separator+"zh_CN"+File.separator+"_LocaleText.csv");
byte[] b = new byte[3];
fis.read(b);
String code = "GBK";
if (b[0] == -17 && b[1] == -69 && b[2] == -65) {
code = "UTF-8";
}
InputStreamReader isw = new InputStreamReader(fis, code);
try (BufferedReader br = new BufferedReader(isw)) {
boolean bReadNext = true;
while (bReadNext) {
i++;
// 一行
String strReadLine = br.readLine();
if (strReadLine == null) {
break;
}
String[] ss = strReadLine.split(",");
String str = ss[0];
int id = Integer.parseInt(str);
str = strReadLine.replaceFirst(str, "");
str = str.replaceFirst(",", "");
if (ss.length > 2) {
int length = str.length();
str = str.substring(1, length - 1);
}
languages.put(id, str);
languaged.put(str, id);
}
}
} catch (IOException | NumberFormatException e) {
log.error(e);
}
System.out.println(languaged.toString());
return languages;
}
添加回复
请先登陆