this.getClass().getResourceAsStream() reads data

this.getClass().getResourceAsStream() reads data

(2014-11-05 13:35:13)

http://imsnoweagle.blog.sohu.com/108074172.html
In JAVA, it is more appropriate to use this.getClass().getResourceAsStream() to read the file data of class configuration resources.
The path adopts a relative path, which can be found directly from the path path of the project. The main problem is that if the static block is used in the class, the this.getClass() will report an error, because when reading the static block, the object may not be constructed, so using this to point to the current object will not work.
ClassLoader provides two methods for obtaining resources from the loaded classpath:
  public URL getResource(String name);
  public InputStream getResourceAsStream(Stringname);
where name is the classpath of the resource, which is relative to the "/" root path s position. What getResource gets is a URL object to locate the resource, and getResourceAsStream gets a reference to the input stream of the resource to ensure that the program can extract data from the correct location.
For example:
 mypackage.Hello.class.getResourceAsStream("/config/config.ini");
From the config relative path under the classpath, read
the file data of config.ini JAVA for calling the class configuration resource, use this.getClass ().getResourceAsStream() is more appropriate to read.

 

http://baoqiang2004.blog.163.com/blog/static/16819372920111130110722/
(1).new FileInputStream(“a.txt”)
Then, this a.txt should be in the root directory of the project.
(2). this.getClass().getClassLoader().getResourceAsStream("a.txt")
in the bin directory, or where the class is loaded – outside the package
(3).this.getClass().getResourceAsStream("a.txt")
in In the bin directory, or where the class is loaded –
getResourceAsStream in the package: its essence is to load the location of the configuration file according to the location of the class,

 

http://ouyangfei0426.iteye.com/blog/1020232

http://blog.csdn.net/zhanghaipeng1989/article/details/19332489
Class.getResourceAsStream() will specify that the resource path to be loaded is the same as the path of the package where the current class is located.
For example, if you write a MyTest class under the package com.test.mycode, MyTest.class.getResourceAsStream ("name") will look for the corresponding resource under the com.test.mycode package .
If the name starts with '/', the search will start from the root of the classpath .
1. Class.getResourceAsStream(String path) :
If the path does not start with '/', the default is to download resources from the package where this class is located, and if the path starts with '/', it is obtained from the root of ClassPath (Src root directory).
It just constructs an absolute path through path, and finally obtains resources from ClassLoader.
    

http://hi.baidu.com/lzpsky/item/6ae30e5dec024c3494eb0526


Project file structure:
testjee
  src
     config
         config1.properties
     configproperty
         ConfigPropManage.java
        config2.properties
        config
            config4.properties
     config3.properties

Code:

package configproperty;
public class ConfigPropManage {
 private static ConfigPropManage instance = newConfigPropManage();
 private ConfigPropManage() {
 }
 public static ConfigPropManage getInstance(){
  return instance;
 }
 
  public static void main(String[] args) {
  ConfigPropManage instance =ConfigPropManage.getInstance();

   // The current package path, the URI directory of the current class class file is obtained. Exclude yourself
  //out:file:/D:/workplace-Ec/workspace_jee/testjee/bin/configproperty/
  System.out.println(instance.getClass().getResource(""));
    // current classpath, What is obtained is the absolute URI path of the current classpath
  //out:file:/D:/workplace-Ec/workspace_jee/testjee/bin/
  System.out.println(instance.getClass().getResource("/"));
  

 ////////////////////////////////////////////config1. Properties ////////////////////////////////
  // No "/", indicating the package configproperty from the current class Find the resource config/config1.properties. Not found.
  //out: 11:null
  System.out.println("11:"+instance.getClass().getResourceAsStream("config/config1.properties"));
  
  //Add "/", it means from class The path is to find resources from the classes folder and find them.
  //out: 12: java.io.BufferedInputStream@37a71e93
  System.out.println("12:"+instance.getClass().getResourceAsStream("/config/config1.properties"));
  
  //Add "/", It means that the resource is searched from the classpath, that is, from the classes folder, and is not found.
  System.out.println("13:"+instance.getClass().getResourceAsStream("/config1.properties"));
  
  //If "/" is not added, it means to find the resource config1 from the package configproperty where the current class is located .properties. did not find.
  System.out.println("14:"+instance.getClass().getResourceAsStream("config1.properties"));
  
  //Add "/" to find resources from the classpath, that is, from the classes folder, did not find.
  System.out.println("15:"+instance.getClass().getResource("/config1.properties")); 
  
  //If "/" is not added, it means to find the resource config1 from the package configproperty where the current class is located .properties. Not found.
  System.out.println("16:"+instance.getClass().getResource("config1.properties")); 
  
  //Add "/", it means from the class path, that is, from the classes folder Looking for resources, but not found.
  //out:17:file:/D:/workplace-Ec/workspace_jee/testjee/bin/config/config1.properties
  System.out.println("17:"+instance.getClass().getResource("/config/ config1.properties")); 
  
  //If "/" is not added, it means to find the resource config/config1.properties from the package configproperty where the current class is located. did not find.
  System.out.println("18:"+instance.getClass().getResource("config/config1.properties"));  
 

 ////////////////////////////////////////////config2. properties///////////////////////////////
  //Add "/", it means from the classpath Find resources from the classes folder, but not found.
  System.out.println("23:"+instance.getClass().getResourceAsStream("/config2.properties"));
  
  //If "/" is not added before the file name, it means that the current class is located under the package configproperty to find the Resource config2.properties. Find.
  //out: 24: java.io.BufferedInputStream@7c3df479
  System.out.println("24:"+instance.getClass().getResourceAsStream("config2.properties"));
  
  //Add "/", then Indicates that the resource is searched from the classpath, that is, from the classes folder, and is not found.
  System.out.println("25:"+instance.getClass().getResource("/config2.properties")); 
  
  //If "/" is not added before the file name, it means that the current class is located under the package configproperty to find the Resource config2.properties. turn up
  //out:26:file:/D:/workplace-Ec/workspace_jee/testjee/bin/configproperty/config2.properties
  System.out.println("26:"+instance.getClass().getResource("config2.properties"));   

 

 ////////////////////////////////////////////config3. properties///////////////////////////////
   //Add "/", it means from the classpath, that is, from Find the resource config3 in the classes folder and find it.
    //out:33: java.io.BufferedInputStream@7106e68e
  System.out.println("33:"+instance.getClass().getResourceAsStream("/config3.properties"));
  
  //Do not add "/" before the file name , it means to find the resource config3.properties from the package configproperty where the current class is located.
  System.out.println("34:"+instance.getClass().getResourceAsStream("config3.properties")); //
  
  Add "/", it means to search from the classpath, that is, from the classes folder Resource config3, found.
  //out:35:file:/D:/workplace-Ec/workspace_jee/testjee/bin/config3.properties
  System.out.println("35:"+instance.getClass().getResource("/config3.properties" ));  
  
  //If "/" is not added before the file name, it means to find the resource config3.properties from the package configproperty where the current class is located. did not find
  System.out.println("36:"+instance.getClass().getResource("config3.properties"));  

 

 ////////////////////////////////////////////config4. properties/////////////////////////////////////////////////////////////////////////////////
  // Without "/", it means the current class Find the resource config/config4.properties under the package configproperty where it is located. Find
  //out: 41: java.io.BufferedInputStream@7e6cbb7a
  System.out.println("41:"+instance.getClass().getResourceAsStream("config/config4.properties"));
  
  //add "/", It means that the resource config/config4.properties is searched from the classpath, that is, from the classes folder, and is not found.
  System.out.println("42:"+instance.getClass().getResourceAsStream("/config/config4.properties"));
  
  //Add "/", it means from the class path, that is, from the classes folder Look for resource config4.properties, but not found.
  System.out.println("43:"+instance.getClass().getResourceAsStream("/config4.properties"));
  
  //If "/" is not added before the file name, it means to find the file from the package configproperty where the current class is located resource config4. properties. did not find
  System.out.println("44:"+instance.getClass().getResourceAsStream("config4.properties"));
  
  //Add "/" to find resources from the classpath, that is, from the classes folder, did not find.
  System.out.println("45:"+instance.getClass().getResource("/config4.properties")); 
  
  //If "/" is not added before the file name, it means that the current class is located under the package configproperty to find the Resource config4.properties.
  System.out.println("46:"+instance.getClass().getResource("config4.properties"));  //
  
  Add "/", it means to search from the classpath, that is, from the classes folder resource, not found.
  System.out.println("47:"+instance.getClass().getResource("/config/config4.properties")); 
  
  //If "/" is not added, it means to find this class from the package configproperty where the current class is located Resource config/config4.properties. Found
  //out:48:file:/D:/workplace-Ec/workspace_jee/testjee/bin/configproperty/config/config4.properties
  System.out.println("48:"+instance.getClass().getResource("config/config4.properties")); 

 }
 }


Doubt:
      //If the current package under the classpath has a 22.properties file, then output: 6:file:/E:/myobject/myspider/build/classes/myspider/22.properties 
     //Otherwise, output under the source file 22. The path of the properties file, then output: 6: file:/E:/myobject/myspider/src/myspider/22.properties 
    System.out.println("6:"+t.getClass().getResource("22 .properties"));  
 
    //If there is a 11.properties file in the classpath, output 7: file:/E:/myobject/myspider/build/classes/11.properties 
    //Otherwise, output 11. The path of the properties file, then output: 7: file:/E:/myobject/myspider/src/11.properties 
   System.out.println("7:"+t.getClass().getResource("/11.properties" ));

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325935827&siteId=291194637