开发者

Are there any problems with creating large static resource files? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.

Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.

Closed 9 years ago.

Improve this question

Is it acceptable to create large static resource files? For example:

public class Resources {
    public static String STRING1 = "xxx";
    public static String STRING2 = "xxx";
    ...
    public static String STRINGN = "xxx";
}

I want to have a large file that will have a list of all of the strings that the application will use. Will this cause problems? I'm not entirely sure how static classes are held in memory, so I don't know if this causes memory issues. Is it too "ugly" or not good f开发者_如何学JAVAorm? Is there a better way to do it?


I would save language files and such things in the filesystem as plain text (or some special format). Then you can change the text, etc. easily. Then you can write a static class (LanguageFactory) to get your strings.

Example language file:

English language file

!--OutGameMenu Strings--!

joinGame = Join Game createGame = Create Game

LanguageFactory:

public class LanguageFactory {
    private static final String PREFIX = "/languages/";
    public static final int ENGLISH = 1;
    public static final int GERMAN = 2;
    public static final int DEFAULT = ENGLISH;
    //List of available InProperties:

    public static final String JOIN_GAME = "joinGame";
    public static final String CREATE_GAME = "createGame";

    private static Properties language;

    public static String getString(String text){
        if(language == null){
            setLanguage(DEFAULT);
        }
        return language.getProperty(text);
    }

    public static void setLanguage(int language){
        switch (language) {
        case ENGLISH:
            setLanguage("en.lang");
            break;
        case GERMAN:
            setLanguage("de.lang");
            break;
        }
    }

    private static void setLanguage(String path){
        language = new Properties();
        try {
            InputStream fi = LanguageFactory.class.getResourceAsStream((PREFIX+path));
            language.load(fi);
            fi.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


It will cause problems if the size of the file is significant relative to the amount of available working memory (Old Gen) you have allocated.

Presumably you're going to load the contents of the file into some sort of Map?

If you need to be able to access the data in the file very quickly then having an in memory representation makes sense.


I would stay with properties-files. (Like the NetBeans IDE does for instance.) There is sufficient tooling around for internationalisation. You always can migrate later. First there is a dynamic ListResourceBundle. And then other solutions (like your own DB base ResourceBundle).

I even would not wrap it, so you may use IDE support ( //NOI18N and such). Better spend your time on organizing the i18n with a glossary, phrase booklet ("Cannot open xxx document."). An issue with properties files is that the encoding is ISO-8859-1 with \u escapes. My solution is a maven plugin to copy the resources filtered from UTF-8 Unicode. And edit all sources in UTF-8.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜