在maven项目结构下对于Resources目录下文件的存取
2024-08-30 23:54:45
在maven项目中,文件结构如下:
proj
---src
----main
----java
----Main.java
----resources
----userFile.properties
----test
----java
----Test.java
----resources
----userFile.properties
如果在程序中要读取resources目录下面的res.txt文件,需要用到以下代码:
- private Properties loadUser() throws Exception{
- if(userList == null){
- InputStream in = this.getClass().getResourceAsStream("/userFile.properties");
- if(in == null)
- throw new Exception("Can not find user file");
- else{
- userList = new Properties();
- userList.load(in);
- }
- }
- return userList;
- }
- private boolean saveUserList() throws IOException{
- if(userList == null)
- return false;
- String url = this.getClass().getResource("/userFile.properties").getFile();
- userList.store(new FileOutputStream(url), "Users Info List");
- return true;
- }
用于测试的resource 文件放在test/reources/ 目录下,用于正式运行的文件放在main/resources/ 目录下。
最新文章
- CentOS6.7搭建LNMP环境
- 也谈JavaScript闭包
- 大一上C语言期末大作业-成绩管理系统
- TOPAPI 消息通知机制
- Kali Linux 2.0: 安装之后的操作
- WEB服务器2--IIS架构(转)
- javadoc时候乱码-编码 GBK 的不可映射字符 - wqjsir的专栏 - 博客频道 - CSDN.NET
- tp框架设置 mysql数据库的端口号
- docker 安装NexusRepository Manager
- 删除倒数第k个元素
- git 入门教程之忽略文件
- Log4j2配置与使用
- Elasticsearch5.4署遇到的问题
- JVM总结-垃圾回收(下)
- 深度学习原理与框架-Tensorflow卷积神经网络-神经网络mnist分类
- XJad反编译工具
- Entity Framework Plus
- 浅用block 转
- bzoj1997
- Linux命令应用大词典-第18章 磁盘分区