NutzCN Logo
问答 在启服时,在MainSetup模块中初始化加载语言模块下某一文件,怎么获取他的绝对路径?
发布于 2686天前 作者 qq_e86802e0 3543 次浏览 复制 上一个帖子 下一个帖子
标签: mvc

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;
}

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;
    }

不打算用nutz内置的吗?

另外,这个文件在classpath下, 直接读文件不好啊

InputStream fis = getClass.getClassLoader().getResouceAsStream("language/zh_CN/_LocaleText.csv");

如果用nutz内置,怎么使用?

_LocaleText.csv这个文件的格式和我们常用的配置格式不一样呢,

哦,那算了

添加回复
请先登陆
回到顶部