我已经在Eclipse中创建了一个动态Web项目,并且已经编写了一个简单的代码来读取属性文件。
这里是我的代码从属性文件中读取值
public class AutocompleteService {
public static void main (String args[])
{
Properties properties = new Properties();
properties.load(new FileInputStream("autocomplete.properties"));
System.out.println("Test : " + properties.getProperty("test"));
}
当我运行此我得到的文件未发现异常。
java.io.FileNotFoundException: autocomplete.properties (The system cannot find the file specified)
我的包结构如下
-src
- com.serive (package)
- AutocompleteService.java
- autocomplete.properties
两个AutocompleteService.java
和autocomplete.properties
都在同一个包即com.service
。做我们需要别的从性能读取文件?
编号:http://www.exampledepot.com/egs/java.util/Props.html
〜Ajinkya。
非常感谢:)它工作并且比其他人更简单。:) – xyz
虽然应该是'getResourceAsStream'。 – Thilo
@Thilo:是的,它应该是。我用'properties.load(AutocompleteService.class.getResourceAsStream(“autocomplete.properties”));' – xyz