我目前有以下方法,可以让我选择在resources / config.properties文件中定义的属性
<Grid xs={3}>
<Button>heading</Button>
<Button>Heading2</Button>
</Grid>
<Grid xs={3}>
<h1>heading</h1>
<p>Paragraph</p>
<h2>Heading2</h2>
<p>Paragraph2</p>
</Grid>
但是我现在想从类路径中选择我的属性,因此我将config.properties从资源中移出并将其直接放在src下。但是我很难知道我现在需要对方法进行哪些更改才能使我能够从类路径中读取。
答案 0 :(得分:1)
Check example here
import java.io.IOError;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class ReaderProp {
public final Properties properties = new Properties();
public ReaderProp()
{
final ClassLoader loader = getClass().getClassLoader();
try(InputStream config = loader.getResourceAsStream("error.properties")){
properties.load(config);
} catch(IOException e){
throw new IOError(e);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ReaderProp readerProp = new ReaderProp();
System.out.println(readerProp.properties.get("E1000_SE_ERROR-CODE"));// output E1000
}
}
Check error.properties
======================
E1000_SE_ERROR-CODE = E1000
答案 1 :(得分:0)
//找到另一个答案
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class ReaderProp {
private final Properties configProp = new Properties();
/**
* read property file
*
* @param propertyName
* @param path
*
* @throws IOException
*/
public ReaderProp(String propertyName, String path) {
try {
InputStream in;
File file;
if (path.equals("")) {
in = this.getClass().getClassLoader().getResourceAsStream(propertyName);
}
else {
file = new File(path + propertyName);
in = new FileInputStream(file);
}
configProp.load(in);
}
catch (IOException e) {
}
}
public static void main(String[] args) {
ReaderProp readerProp = new ReaderProp("error.properties",new File("").getAbsolutePath()+"\\src\\");
System.out.println(readerProp.configProp.get("E1000_SE_ERROR-CODE"));// output E1000
}
}