Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
348 views
in Technique[技术] by (71.8m points)

java - getResourceAsStream() is returning null. Properties file is not loading

I am trying to load properties file. Here is my structure

Directory Structure

Now i am trying to load test.properties file. But i am getting null. Here how i am doing

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

This program prints

Current working directory : D:Personal Workeclipse 32 BitworkspaceSpring IntegrationLS360BatchImportIntegration
File path : D:Personal Workeclipse 32 BitworkspaceSpring IntegrationLS360BatchImportIntegrationest.properties

But it is not loading properties file from this path. Although it is present there. Why i am getting null ?

Thanks

Edit--- ----------------------------

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 Workeclipse 32 BitworkspaceSpring IntegrationLS360BatchImportIntegration
File path : D:Personal Workeclipse 32 BitworkspaceSpring IntegrationLS360BatchImportIntegrationest.properties
java.io.FileNotFoundException: D:Personal Workeclipse 32 BitworkspaceSpring IntegrationLS360BatchImportIntegrationest.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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Oh oh ... There are several problems here:

1) In your first provided code snippet, you are using a ClassLoader for loading a resource file. This is indeed a good decision. But the getResourceAsStream method needs a "class-path relative" name. You are providing an absolute path.

2) Your second code snippet (after edit) results in not being able to find the file "D:...LS360BatchImportIntegrationest.properties". According to your screenshot, the file should be "D:...LS360AutomatedRegulatorsReportingServiceest.properties". This is another directory.

I fear, that your descriptions are not up to date with the findings on your machine.

But let's just move to a reasonable solution:

1) In your Eclipse project (the screenshot tells us, that you are using Eclipse), create a new directory named "resources" in the same depth as your "src" directory. Copy - or better move - the properties file into it.

2) This new directory must be put into the "build path". Right-click the directory in the Package Explorer or Project Explorer view, select "Build Path", then "Use as Source Folder". Note: This build path will be the class path for the project, when you run it.

3) As the resources directory now is part of your class path and contains your properties file, you can simply load it with getResourceAsStream("test.properties").

EDIT

I just see, that you also use Maven (the pom.xml file). In Maven, such a resources directory exists by default and is part of the build path. It is "src/main/resources". If so, just use this.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...