项目中的参数化配置
import java.io.IOException; import java.io.InputStream; import java.util.Properties; import com.retail.supmarket.http.controller.SweepCodePaymentController; public class ReadFileUtil { /** * * @Title: readFile 从properties文件中读取url的配置信息 * @Description: TODO String * @author Administrator */ public static String readFile(String proper) { Properties properties = new Properties(); // 使用ClassLoader加载properties配置文件生成对应的输入流 InputStream in = SweepCodePaymentController.class.getClassLoader().getResourceAsStream("url.properties"); // 使用properties对象加载输入流 try { properties.load(in); } catch (IOException e) { System.out.println("请在配置中检查url的配置是否正确" + e); e.printStackTrace(); } // 获取key对应的value值 String getHttpProperty = properties.getProperty(proper); return getHttpProperty; } }
import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Properties; public class ReadFileUtil { private static final String DEFAULT_ENCODING = "UTF-8"; /** * * @Title: readFile 从properties文件中读取url的配置信息 * @Description: TODO String * @author Administrator */ public static String readFile(String proper) { try { // 获取文件流(方法1或2均可) //InputStream inputStream = new BufferedInputStream( //new FileInputStream(new File("/src/main/resources/url.properties"))); // 方法1 InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("url.properties"); // //方法2 Properties prop = new Properties(); prop.load(new InputStreamReader(inputStream, DEFAULT_ENCODING)); // 加载格式化后的流 String driverClassName = prop.getProperty(proper); return driverClassName; } catch (FileNotFoundException e) { System.out.println("properties文件路径有误!"); e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return proper; } }
java读取资源参考博客:
https://www.cnblogs.com/shuimuzhushui/p/7247864.html
在jar文件下面读取:
https://www.cnblogs.com/TonyYPZhang/p/6298422.html
用类加载读取配置文件:
https://www.cnblogs.com/1540340840qls/p/6184109.html
本文来自博客园,作者:diligently,转载请注明原文链接:https://www.cnblogs.com/luo12828-foxmail/p/16964119.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY