我想存储文件名,随着新文件的添加,文件名会不断变化。当需要支持新的"file"时,我正在寻找服务器代码的最小更改,我的想法是将它们存储在属性文件中或作为 Java 枚举,但仍然认为哪个是更好的方法。
我正在使用 REST 并在 URL 中包含“文件类型”。 其余网址示例:
主机名/文件内容/类型
其中 TYPE 的值可以是以下任意一个:standardFileNames1、standardFileNames2、randomFileName1、randomFileName2
我使用了 TYPE 对文件进行分组,以便在添加新文件时最小化 url 的变化。由于安全问题,不希望 URL 中包含文件名。
我的想法是这样的:
具有 ENUM:
public enum FileType
{
standardFileNames1("Afile_en", "Afile_jp"),
standardFileNames2("Bfile_en","Bfile_jp"),
randomFileName1("xyz"),
randomFileName2("abc"),
...
...
}
具有属性文件:
standardFileNames1=Afile_en,Afile_jp
standardFileNames2=Bfile_en,Bfile_jp
randomFileName1=xyz
randomFileName2=abc
我知道在属性中包含此内容将节省每次更改的构建工作,但仍然想了解您的观点,以便在考虑所有因素后找出最佳解决方案。
谢谢! 阿基勒什
最佳答案
I often use property file + enum combination. Here is an example:
public enum Constants {
PROP1,
PROP2;
private static final String PATH = "/constants.properties";
private static final Logger logger = LoggerFactory.getLogger(Constants.class);
private static Properties properties;
private String value;
private void init() {
if (properties == null) {
properties = new Properties();
try {
properties.load(Constants.class.getResourceAsStream(PATH));
}
catch (Exception e) {
logger.error("Unable to load " + PATH + " file from classpath.", e);
System.exit(1);
}
}
value = (String) properties.get(this.toString());
}
public String getValue() {
if (value == null) {
init();
}
return value;
}
}
Now you also need a property file (I ofter place it in src, so it is packaged into JAR), with properties just as you used in enum. For example:
constants.properties:
#This is property file...
PROP1=some text
PROP2=some other text
Now I very often use static import in classes where I want to use my constants:
import static com.some.package.Constants.*;
And an example usage
System.out.println(PROP1);
Source:http://stackoverflow.com/questions/4908973/java-property-file-as-enum
关于java - 我应该将文件名存储在 Java 枚举还是属性文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14658400/