在maven项目中,文件结构如下:

proj
---src
----main
----java
----Main.java
----resources
----userFile.properties
----test
----java
----Test.java
----resources
----userFile.properties

如果在程序中要读取resources目录下面的res.txt文件,需要用到以下代码:

  1. private Properties loadUser() throws Exception{
  2. if(userList == null){
  3. InputStream in = this.getClass().getResourceAsStream("/userFile.properties");
  4. if(in == null)
  5. throw new Exception("Can not find user file");
  6. else{
  7. userList = new Properties();
  8. userList.load(in);
  9. }
  10. }
  11. return userList;
  12. }
  1. private boolean saveUserList() throws IOException{
  2. if(userList == null)
  3. return false;
  4. String url = this.getClass().getResource("/userFile.properties").getFile();
  5. userList.store(new FileOutputStream(url), "Users Info List");
  6. return true;
  7. }

用于测试的resource 文件放在test/reources/ 目录下,用于正式运行的文件放在main/resources/ 目录下。

最新文章

  1. CentOS6.7搭建LNMP环境
  2. 也谈JavaScript闭包
  3. 大一上C语言期末大作业-成绩管理系统
  4. TOPAPI 消息通知机制
  5. Kali Linux 2.0: 安装之后的操作
  6. WEB服务器2--IIS架构(转)
  7. javadoc时候乱码-编码 GBK 的不可映射字符 - wqjsir的专栏 - 博客频道 - CSDN.NET
  8. tp框架设置 mysql数据库的端口号
  9. docker 安装NexusRepository Manager
  10. 删除倒数第k个元素
  11. git 入门教程之忽略文件
  12. Log4j2配置与使用
  13. Elasticsearch5.4署遇到的问题
  14. JVM总结-垃圾回收(下)
  15. 深度学习原理与框架-Tensorflow卷积神经网络-神经网络mnist分类
  16. XJad反编译工具
  17. Entity Framework Plus
  18. 浅用block 转
  19. bzoj1997
  20. Linux命令应用大词典-第18章 磁盘分区

热门文章

  1. POJ3253 Fence Repair【贪心】
  2. case...when...then if 用法
  3. Linux—Ubuntu14.0.5设置MySQL的字符集
  4. NFS实时备份
  5. jQuery cxCalendar 日期选择器
  6. BZOJ 3329 Xorequ (数位DP、矩阵乘法)
  7. Nexus私服的搭建
  8. States of Integrity Constraints
  9. Bi-shoe and Phi-shoe 欧拉函数 素数
  10. 关于git修改和查看用户名邮箱