首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Web工程下读取文件的几种方法

往期精选

1、读取文件系统路径文件 * 一定要使用绝对路径

String fileName = getServletContext().getRealPath("/index.jsp"); // 读取WebRoot下

String fileName2 = getServletContext().getRealPath(

"/WEB-INF/a.properties");// 读取WebRoot/WEB-INF/a.properties

2、读取classpath下文件

String fileName3 = ReadFile.class.getResource("/info.txt").getFile();// 读取src下info.txt

String fileName4 = ReadFile.class.getResource(

"/cn/itcast/config/info.txt").getFile();// 读取 src下

// cn.itcast.config包下info.txt

3、使用ResourceBundle 快速读取src下properties文件

String value = ResourceBundle.getBundle("myproperties").getString(

"name");// 读取src下 基名为myproperties的properties文件,获取其中name配置值

4、使用Properties类加载Properties文件

// 读取src下b.properties

InputStream in = ReadFile.class.getResourceAsStream("/b.properties");

Properties properties = new Properties();

properties.load(in);

String value2 = properties.getProperty("name"); // 获得name属性

  • 发表于:
  • 原文链接http://kuaibao.qq.com/s/20171218B09UL000?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券