1:方式1:

public static List<String> userList;
static {
userList = new LinkedList<String>();
try {
**String filePath = TestClient.class.getClassLoader().getResource("users.txt").getPath();**
**BufferedReader reader = new BufferedReader(new FileReader(new File(filePath)));**
try {
String line = null;
while ((line = reader.readLine()) != null) {
userList.add(line);
}
} finally {
reader.close();
}
}catch (Exception e){
e.printStackTrace();
}

2:方式2:

public staitc List<String> userList;
static {
userList = new LinkedList<String>();
try {
**InputStream is = TestClient.class.getResourceAsStream("/user.txt");**
**BufferedReader reader = new BufferedReader(new InputStreamReader(is));**
try {
String line = null;
while ((line = reader.readLine()) != null) {
userList.add(line);
}
} finally {
reader.close();
}
}catch (Exception e){
e.printStackTrace();
}

3:Other Demos

    import java.io.InputStream;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle; public class PropValue {
private static String BASE_URL;
static {
ResourceBundle rb=null;
try {
InputStream inputStream = PropValue.class.getResourceAsStream("/boot.properties");
rb = new PropertyResourceBundle(inputStream);
BASE_URL=rb.getString("boot_url");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args){
System.out.println(PropValue.BASE_URL);
}
}

Maven 工程读取resource下的文件的更多相关文章

  1. maven工程读取resource下配置文件

    maven工程读取resource下配置文件 在maven工程中,我们会将配置文件放到,src/main/resources   下面,例如 我们需要确认resource 下的文件 编译之后存放的位置 ...

  2. SpringBoot项目构建成jar运行后,如何正确读取resource下的文件

    SpringBoot项目构建成jar运行后,如何正确读取resource下的文件 不管你使用的是SpringBoot 1.x还是SpringBoot2.x,在开Dev环境中使用eclipse.IEAD ...

  3. Spring项目读取resource下的文件

    目录 一.前提条件 二.使用ClassPathResource类读取 2.1.Controller.service中使用ClassPathResource 2.2.单元测试使用ClassPathRes ...

  4. springboot读取resource下的文件

    public static String DEFAULT_CFGFILE = ConfigManager.class.getClassLoader().getResource("conf/s ...

  5. springboot 读取 resource 下的文件

    ClassPathResource classPathResource = new ClassPathResource("template/demo/200000168-check-resp ...

  6. SpringBoot读取Resource下文件的几种方式(十五)

    需求:提供接口下载resources目录下的模板文件,(或者读取resources下的文件)给后续批量导入数据提供模板文件. 方式一:ClassPathResource //获取模板文件:注意此处需要 ...

  7. maven工程中防止mapper.xml文件被漏掉、未加载的方法

    maven工程中防止mapper.xml文件被漏掉.未加载的方法 就是在pom.xml文件中添加以下内容 <!-- 如果不添加此节点mybatis的mapper.xml文件都会被漏掉. --&g ...

  8. xcode自动刷新resource下的文件

    修改resource下的lua或者ccbi文件时,xcode并不会察觉到,所以需要手动清理xcode缓存和模拟器缓存,开发效率比较低下. 通过以下步骤可以实现自动刷新resource下的文件,且无需手 ...

  9. Maven项目读取resources下文件的路径问题(getClassLoader的作用)

    读取resources下文件的方法 网上有问答如下:问: new FileInputStream("src/main/resources/all.properties") new ...

随机推荐

  1. 【Java】-NO.13.Algorithm.1.Java Algorithm.1.001-【Java 常用算法手册 】-

    1.0.0 Summary Tittle:[Java]-NO.13.Algorithm.1.Java Algorithm.1.001-[Java 常用算法手册 ]- Style:Java Series ...

  2. 【Python】-NO.96.Note.2.Python -【Python 基础】

    1.0.0 Summary Tittle:[Python]-NO.95.Note.1.Python -[Python 老男孩 基础]- Style:Python Series:Python Since ...

  3. jsp内置对象学习记录

    1.session,是一个会话保留在服务器端的对象(默认保留时间为30分钟),所以我们可以在session里面放用户信息以便后续的访问便利(缺点:cookie劫持,导致用户数据泄露).案例:(1)同个 ...

  4. CentOS6.5 安装Kafka集群

    1.安装zookeeper 参考文档:http://www.cnblogs.com/hunttown/p/5452138.html 2.下载:https://www.apache.org/dyn/cl ...

  5. 利用yum升级Centos6的gcc版本,使其支持C++11

    下面的可以在centos6下工作,centos7下有问题.可能是因为centos下的scl我是拷贝的文件,没有完全验证centos6下肯定没问题. https://my.oschina.net/u/5 ...

  6. undefined reference 问题各种情况分析

    扒自网友文章 关于undefined reference这样的问题,大家其实经常会遇到,在此,我以详细地示例给出常见错误的各种原因以及解决方法,希望对初学者有所帮助. 1.  链接时缺失了相关目标文件 ...

  7. HDU 2254 奥运(矩阵+二分等比求和)

    奥运 [题目链接]奥运 [题目类型]矩阵+二分等比求和 &题解: 首先离散化城市,之后就是矩阵快速幂了,但让求的是A^(t1)+A^(t1+1)+...+A^(t2),我先想的是打表,但时间真 ...

  8. 关于调用数据库函数executeUpdate抛出异常

    2018.06.11写一个web程序的时候发现了一个问题,解决了好几天都没解决,并且也找不到问题所在.偶然百度找到了根源所在,希望可以帮到大家. 1 在调用这个函数的时候一直抛异常.刚开始我还以为是代 ...

  9. .NET 黑魔法 - asp.net core 配置文件的"对象存储"

    来,全都是干货. 我们都知道在Framework版本的mvc项目中,配置数据是通过web.config里的appSettings节点配置,我们不得不写一些读取配置文件字符串的类,比如保存在静态的变量中 ...

  10. CoreWebApi集成Exceptionless

    参考博客 https://www.cnblogs.com/akaxb/p/7207827.html 一. Exceptionlessr的安装部署 依乐祝,<ASP.NET Core免费开源分布式 ...