2013-05-29 34 views
6

Tôi cố gắng để đọc một tập tin .properties và có mã như sau:Nơi đặt tệp thuộc tính?

public final class Config { 
    static { 
    Properties properties = new Properties(); 
    InputStream propertiesStream = Object.class.getResourceAsStream("config.properties"); 

    if (propertiesStream != null) { 
     try { 
     properties.load(propertiesStream); 
     } catch (IOException e) { 
     e.printStackTrace(); 
     } 
    } else { 
     System.out.println("file not found"); 
    } 
    } 
} 

nhưng nó giữ tập tin không tìm thấy nói.

Nội dung của thuộc tính là

pwd=passw0rd 

Bất cứ ai cũng biết làm thế nào để giải quyết vấn đề này?

Trả lời

15

Nó phải ở trong classpath, đặt nó vào gói nguồn gốc của bạn, nếu nó là một dự án maven đặt nó để src/main/resources thư mục

0

nó phải ở trong WebContent/Web-Inf/ thư mục

và bạn tập tin xml xác định đậu như thế này:

<bean id="propertyConfigurer" class="com.your.project.util.properties.ApplicationProperties"> 
    <property name="locations"> 
     <list> 
      <value>/WEB-INF/application.properties</value> 
     </list> 
    </property> 
</bean> 
+0

nếu khuôn khổ bạn đang sử dụng là mùa xuân, thì đây sẽ làm việc tốt –

0

Bạn cũng có thể giữ config.properties trong cùng thư mục với Config.java.

//InputStream propertiesStream = Object.class.getResourceAsStream("config.properties"); 
InputStream propertiesStream = Config.class.getResourceAsStream("config.properties"); 
0

Bạn có thể có hai lựa chọn lựa chọn con đường,

  1. mở file chứa thư mục và lấy đường dẫn và lưu con đường đó trong chuỗi với tập tin như thế nào,

    InputStream propertiesStream = Object.class.getResourceAsStream (đường dẫn + File.seperator + "config.properties");

  2. Save the file trong đường dẫn src,

    WorkSpace -> Tên dự án -> Copyhere

Các vấn đề liên quan