获得InputStream,读取配置文件的方式

转自: 姜兴琪 的 《获得InputStream,读取配置文件的方式》

原址:https://blog.csdn.net/jxq0816/article/details/72461859

一、 获得InputStream的方式

1、使用FileInputStream

InputStream in = new BufferedInputStream(new FileInputStream("E:\\svn_new\\3icommontutil\\src\\main\\resources\\datasource\\datasource.properties"));//绝对路径

/**
     * Creates a <code>FileInputStream</code> by
     * opening a connection to an actual file,
     * the file named by the path name <code>name</code>
     * in the file system.  A new <code>FileDescriptor</code>
     * object is created to represent this file
     * connection.
     * <p>
     * First, if there is a security
     * manager, its <code>checkRead</code> method
     * is called with the <code>name</code> argument
     * as its argument.
     * <p>
     * If the named file does not exist, is a directory rather than a regular
     * file, or for some other reason cannot be opened for reading then a
     * <code>FileNotFoundException</code> is thrown.
     *
     * @param      name   the system-dependent file name.
     * @exception  FileNotFoundException  if the file does not exist,
     *                   is a directory rather than a regular file,
     *                   or for some other reason cannot be opened for
     *                   reading.
     * @exception  SecurityException      if a security manager exists and its
     *               <code>checkRead</code> method denies read access
     *               to the file.
     * @see        java.lang.SecurityManager#checkRead(java.lang.String)
     */
    public FileInputStream(String name) throws FileNotFoundException {
        this(name != null ? new File(name) : null);
    }


2、使用getResourceAsStream

InputStream in = getClass.getResourceAsStream("db.properties"); //path不以’/'开头时默认是从此类所在的包目录下取资源。
InputStream in = getClass.getResourceAsStream("/datasource/db.properties");//以’/'开头则是从ClassPath根目录下获取。

 /**
     * Finds a resource with a given name.  The rules for searching resources
     * associated with a given class are implemented by the defining
     * {@linkplain ClassLoader class loader} of the class.  This method
     * delegates to this object's class loader.  If this object was loaded by
     * the bootstrap class loader, the method delegates to {@link
     * ClassLoader#getSystemResourceAsStream}.
     *
     * <p> Before delegation, an absolute resource name is constructed from the
     * given resource name using this algorithm:
     *
     * <ul>
     *
     * <li> If the {@code name} begins with a {@code '/'}
     * (<tt>'\u002f'</tt>), then the absolute name of the resource is the
     * portion of the {@code name} following the {@code '/'}.
     *
     * <li> Otherwise, the absolute name is of the following form:
     *
     * <blockquote>
     *   {@code modified_package_name/name}
     * </blockquote>
     *
     * <p> Where the {@code modified_package_name} is the package name of this
     * object with {@code '/'} substituted for {@code '.'}
     * (<tt>'\u002e'</tt>).
     *
     * </ul>
     *
     * @param  name name of the desired resource
     * @return      A {@link java.io.InputStream} object or {@code null} if
     *              no resource with this name is found
     * @throws  NullPointerException If {@code name} is {@code null}
     * @since  JDK1.1
     */
     public InputStream getResourceAsStream(String name) {
        name = resolveName(name);
        ClassLoader cl = getClassLoader0();
        if (cl==null) {
            // A system class.
            return ClassLoader.getSystemResourceAsStream(name);
        }
        return cl.getResourceAsStream(name);
    }

 

二、读取配置文件

     Properties prop = new Properties();
     prop.load(in);     ///加载属性列表
     dbName=prop.getProperty("ds.dbName.datasource1");
     serverIp=prop.getProperty("ds.serverIp.datasource1");
     port=Integer.parseInt(prop.getProperty("ds.serverPort.datasource1"));

猜你喜欢

转载自blog.csdn.net/yup1212/article/details/81182272