Tôi đang cố tải tệp thuộc tính. Dưới đây là cấu trúc của tôigetResourceAsStream() trả về giá trị rỗng. Tệp thuộc tính không tải
Bây giờ tôi đang cố gắng để tải test.properties tập tin. Nhưng tôi đang nhận được null. Dưới đây làm thế nào tôi đang làm
public class Test {
String workingDir = System.getProperty("user.dir");
System.out.println("Current working directory : " + workingDir);
File temp = new File(workingDir + "\\" + "test.properties");
String absolutePath = temp.getAbsolutePath();
System.out.println("File path : " + absolutePath);
Properties properties = null;
try {
properties = new Properties();
InputStream resourceAsStream = Test.class.getClassLoader().getResourceAsStream(absolutePath);
if (resourceAsStream != null) {
properties.load(resourceAsStream);
}
} catch (IOException e) {
e.printStackTrace();
}
System.exit(0);
} //end of class Test
Chương trình này in
Current working directory : D:\Personal Work\eclipse 32 Bit\workspace\Spring Integration\LS360BatchImportIntegration
File path : D:\Personal Work\eclipse 32 Bit\workspace\Spring Integration\LS360BatchImportIntegration\test.properties
Nhưng nó không phải đang tải tập tin thuộc tính từ con đường này. Mặc dù nó có mặt ở đó. Tại sao tôi nhận được null?
Cảm ơn
Sửa --- ----------------------------
String workingDir = System.getProperty("user.dir");
System.out.println("Current working directory : " + workingDir);
File temp = new File(workingDir, "test.properties");
String absolutePath = temp.getAbsolutePath();
System.out.println("File path : " + absolutePath);
try {
properties = new Properties();
InputStream resourceAsStream = new FileInputStream(temp);
if (resourceAsStream != null) {
properties.load(resourceAsStream);
}
} catch (IOException e) {
e.printStackTrace();
}
System.exit(0);
Current working directory : D:\Personal Work\eclipse 32 Bit\workspace\Spring Integration\LS360BatchImportIntegration
File path : D:\Personal Work\eclipse 32 Bit\workspace\Spring Integration\LS360BatchImportIntegration\test.properties
java.io.FileNotFoundException: D:\Personal Work\eclipse 32 Bit\workspace\Spring Integration\LS360BatchImportIntegration\test.properties (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at com.softech.ls360.integration.BatchImport.main(BatchImport.java:57)
Có, tôi thấy rằng tôi sao chép tệp 'test.proeprties' ở sai vị trí. Bây giờ nó đang tải từ dòng sau khi đặt nó vào đúng chỗ :). Có tôi đang sử dụng maven. Bạn có nghĩa là di chuyển tập tin thuộc tính vào thư mục 'src/main/resources'. Và thêm thư mục 'src/main/resources' vào đường dẫn lớp bằng cách nhấp chuột phải như bạn đã nói (điểm 2)? – Basit
Tôi nghĩ 'src/main/resources' đã có trên đường dẫn lớp trong maven ..? – Basit
@Basit: Chính xác. "src/main/resources" là thư mục mặc định Maven cho các tài nguyên như vậy. Nếu bạn sử dụng Maven để đóng gói ứng dụng của bạn, nó sẽ được xem xét tự động (và trở thành một phần của JAR của bạn). Nếu bạn chạy dự án trong Eclipse, plugin M2E sẽ xử lý điều đó. – Seelenvirtuose