关于Object.class.getResourceAsStream方法读取文件的使用 您所在的位置:网站首页 php读取文件名 关于Object.class.getResourceAsStream方法读取文件的使用

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

2023-06-16 15:23| 来源: 网络整理| 查看: 265

先附上代码。

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!"); } } }


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有