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

Dear 丶 2023-10-09 23:53 68阅读 0赞

先附上代码。

  1. package com.property;
  2. import java.io.FileNotFoundException;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.util.Enumeration;
  6. import java.util.Properties;
  7. public class Test {
  8. public static int getSocketPort(String tomcatpath, String propertyName) {
  9. Properties prop = new Properties();
  10. InputStream in = null;
  11. String socketPort = null;
  12. try {
  13. // 加载tomcatpath.properties文件
  14. in = Object.class.getResourceAsStream(tomcatpath);
  15. prop.load(in);
  16. Enumeration it = prop.propertyNames();
  17. while (it.hasMoreElements()) {
  18. String key = (String) it.nextElement();
  19. if (propertyName.equals(key)) {
  20. socketPort = prop.getProperty(key);
  21. break;
  22. }
  23. }
  24. } catch (FileNotFoundException e1) {
  25. throw new RuntimeException(e1);
  26. } catch (IOException e) {
  27. // TODO Auto-generated catch block
  28. // e.printStackTrace();
  29. throw new RuntimeException(e);
  30. } finally {
  31. try {
  32. if (in != null) {
  33. in.close();
  34. }
  35. } catch (IOException e) {
  36. // TODO Auto-generated catch block
  37. e.printStackTrace();
  38. }
  39. }
  40. return Integer.parseInt(socketPort);
  41. }
  42. }
  43. package com.property;
  44. public class Main {
  45. public static void main(String[] args) {
  46. int socketPort = Test.getSocketPort("/test/tomcatpath.properties", "tomcat_port");
  47. System.out.println(socketPort);
  48. }
  49. }

在使用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获取资源。

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

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

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

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

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

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

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

  1. public class Bash {
  2. public static void main(String[] args) {
  3. String cmdFileName = "/bash_cmds.properties";
  4. // 获取输入流
  5. InputStream inputStream = Bash.class.getResourceAsStream(cmdFileName);
  6. Properties COMMANDS = new Properties();
  7. try {
  8. // 从输入文件流中获取属性列表
  9. COMMANDS.load(inputStream);
  10. System.out.println(COMMANDS.getProperty("getDeviceDetail"));
  11. System.out.println(COMMANDS.get("modifyIp"));
  12. } catch (IOException e) {
  13. throw new RuntimeException("Load file " + cmdFileName + " failed!");
  14. } catch (NullPointerException npe) {
  15. throw new RuntimeException("File " + cmdFileName + " may not exist in classpath!");
  16. }
  17. }
  18. }

发表评论

表情:
评论列表 (有 0 条评论,68人围观)

还没有评论,来说两句吧...

相关阅读