关于Object.class.getResourceAsStream方法读取文件的使用

先附上代码。

package com.property;
 
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;
 
 
public class Test {
	
	
	public static int getSocketPort(String tomcatpath, String propertyName) {
 
        Properties prop = new Properties();
        InputStream in = null;
        String socketPort = null;
 
        try {
 
            // 加载tomcatpath.properties文件
            in = Object.class.getResourceAsStream(tomcatpath);
            prop.load(in);
 
            Enumeration it = prop.propertyNames();
            while (it.hasMoreElements()) {
                String key = (String) it.nextElement();
 
                if (propertyName.equals(key)) {
                    socketPort = prop.getProperty(key);
                    break;
                }
            }
 
        } catch (FileNotFoundException e1) {
 
            throw new RuntimeException(e1);
 
        } catch (IOException e) {
            // TODO Auto-generated catch block
            // e.printStackTrace();
            throw new RuntimeException(e);
 
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
 
        return Integer.parseInt(socketPort);
    }
}
package com.property;
 
public class Main {
 
	public static void main(String[] args) {
		int socketPort = Test.getSocketPort("/test/tomcatpath.properties", "tomcat_port");
		System.out.println(socketPort);
	}
 
}

在使用Object.class.getResourceAsStream方法时,在src同级目录下创建文件夹configss,文件夹下创建log4j.properties文件和文件夹test,test文件夹下创建文件tomcatpath.properties,如图

在这里插入图片描述

此时,执行main函数,程序会直接报错,经过研究,找出问题如下:

需要将configss文件夹执行build path–use as source folder,结果如下图

img

此时,执行main函数,方法会成功,输出tomcat_port的值。

原因:当将configss文件夹执行build path–use as source folder时,configss文件夹下的配置文件可以被类以相对路径直接读写。(与configss文件夹本身名称无关)

文件 - 获取resource目录下文件的输入流 inputstream: getResourceAsStream()

两种使用方式:

① T.class.getResourceAsStream(path) : path 不以’/'开头时默认是从此类所在的包下取资源,以’/'开头则是从ClassPath根下获取。其只是通过path构造一个绝对路径,最终还是由ClassLoader获取资源。

 String path="/application.yml";
 InputStream resourceAsStream = A.class.getResourceAsStream(path);
 InputStream resourceAsStream = this.getClass().getResourceAsStream(path);

② T.class.getClassLoader().getResourceAsStream(path):默认则是从ClassPath根下获取,path不能以’/'开头,最终是由ClassLoader获取资源。

 String path="/application.yml";
 InputStream resourceAsStream1 = A.class.getClassLoader().getResourceAsStream(path);

③ 从输入文件流中获取属性列表:

在这里插入图片描述
bash_cmds.properties文件:

modifyIp=nmcli con modify "$1" "$2" "$3" ipv4.method manual
getDeviceDetail=nmcli device "$1"

加载bash_cmds.properties文件并获取属性列表:

public class Bash {
    public static void main(String[] args) {
        String cmdFileName = "/bash_cmds.properties";
        // 获取输入流
        InputStream inputStream = Bash.class.getResourceAsStream(cmdFileName);
        Properties COMMANDS = new Properties();
        try {
            // 从输入文件流中获取属性列表
            COMMANDS.load(inputStream);
            System.out.println(COMMANDS.getProperty("getDeviceDetail"));
            System.out.println(COMMANDS.get("modifyIp"));
        } catch (IOException e) {
            throw new RuntimeException("Load file " + cmdFileName + " failed!");
        } catch (NullPointerException npe) {
            throw new RuntimeException("File " + cmdFileName + " may not exist in classpath!");
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Archie_java

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值