我应该在Java枚举或属性文件中存储文件名吗
我想存储文件名,随着新文件的添加,文件名会不断变化。当需要支持一个新的“文件”时,我正在寻找服务器代码中的最小更改。我的想法是将它们存储在属性文件或Java enum中,但仍在考虑哪种方法更好 我正在使用REST,URL中有“文件类型”。 rest url示例: 主机名/文件内容/类型 其中,类型的值可以是以下任意值:standardFileNames1、standardFileNames2、randomFileName1、randomFileName2 我使用TYPE对文件进行分组,以便在添加新文件时最小化url中的更改。由于安全问题,不希望URL中包含文件名 我的想法是这样的: 具有作为枚举的: 作为属性文件具有: 我知道在属性中使用此功能将节省每次更改的构建工作,但我仍然希望了解您的观点,以找出考虑所有因素的最佳解决方案 谢谢!我应该在Java枚举或属性文件中存储文件名吗,java,rest,Java,Rest,我想存储文件名,随着新文件的添加,文件名会不断变化。当需要支持一个新的“文件”时,我正在寻找服务器代码中的最小更改。我的想法是将它们存储在属性文件或Java enum中,但仍在考虑哪种方法更好 我正在使用REST,URL中有“文件类型”。 rest url示例: 主机名/文件内容/类型 其中,类型的值可以是以下任意值:standardFileNames1、standardFileNames2、randomFileName1、randomFileName2 我使用TYPE对文件进行分组,以便在添加
Akhilesh我的建议是保存在属性或配置文件中,并编写一个通用代码来获取文件列表并用java进行解析。因此,每当出现一个新文件时,服务器端不会有任何更改,而是向属性或配置文件添加一个条目
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
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