How to get the file in a resource folder
In a Maven project, we may often struggle to get a certain file (e.g. json file or sql file). Here is how to place the resource file and use it in the java class.
1. If the main class is in folder src/main/java/, the resource file should be placed in the folder src/main/resources/
Suppose the main class is GeneralTest.class and the file to be used is under src/main/resources/stories/simple-upgrades/retry-upgrade-file.story
Then the java program should be as follows
import java.io.File;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; public class GeneralTest { protected String getStoriesDirectoryWithinResources() {
return "stories" + File.separator + "simple-upgrades";
} protected List<String> storyPaths() {
List<String> result = new ArrayList<String>();
String storiesDirectoryWithinResources = getStoriesDirectoryWithinResources();
URL resource = GeneralTest.class.getClassLoader().getResource(storiesDirectoryWithinResources);
System.out.println(resource);
try {
File folder = new File(resource.toURI());
if (folder.exists() && folder.canRead() && folder.isDirectory()) {
File files[] = folder.listFiles();
Arrays.sort(files);
for (File file : files) {
result.add(file.getPath().substring(folder.getPath().lastIndexOf(storiesDirectoryWithinResources)));
}
}
} catch (URISyntaxException e) {
e.printStackTrace();
}
return result;
} public static void main(String... args) {
GeneralTest test = new GeneralTest();
System.out.println(test.storyPaths());
}
}
The output should be:
file:/home/username/workplace/JavaMavenPractice/target/classes/stories/simple-upgrades
[stories/simple-upgrades/retry-upgrade-file.story]
How to get the file in a resource folder的更多相关文章
- springMVC+mybatis 进行单元测试时 main SqlSessionFactoryBean - Parsed configuration file: 'class path resource' 无限的读取xml文件
今天终于写完的Dao层的操作,怀着无比激动的心情,进行单元测试,就在最后一个方法,对的就是最后一个方法,启动单元测试就会报以下错误: [2016-05-11 18:25:01,691] [WARN ] ...
- How to build .apk file from command line(转)
How to build .apk file from command line Created on Wednesday, 29 June 2011 14:32 If you don’t want ...
- Spring源码分析——资源访问利器Resource之实现类分析
今天来分析Spring的资源接口Resource的各个实现类.关于它的接口和抽象类,参见上一篇博文——Spring源码分析——资源访问利器Resource之接口和抽象类分析 一.文件系统资源 File ...
- eclipse中 linked resource的使用
一.关于linked resource eclipse 中的linkded resources 是指存放在项目所在位置以外某个地方的文件或者文件夹:这些特定的资源必须有一个项目作为他们的父资源.l ...
- java Resource
ClassPathResource: String resource = ""; //相对路径 Resource resource = new ClassPathResource( ...
- Linux Found a swap file by the name filename
在Linux中使用vi命令编辑mysql_backup.sh时遇到下面提示信息 E325: ATTENTION Found a swap file by the name ".mysql_b ...
- TN035: Using Multiple Resource Files and Header Files with Visual C++
TN035: Using Multiple Resource Files and Header Files with Visual C++ This note describes how the Vi ...
- 查找EBS中各种文件版本(Finding File Versions in the Oracle Applications EBusiness Suite - Checking the $HEADER)
Finding File Versions in the Oracle Applications EBusiness Suite - Checking the $HEADER (文档 ID 85895 ...
- Spring Resource之内置的Resource实现
Spring提供了大量的并且可以直接使用的Resource实现 1.UrlResource UrlResource封装了一个java.net.URL,而且可以通过一个URL用于访问任何对象,例如文件. ...
随机推荐
- Linux常用命令整理
1.常用命令:cd 进入 ls(list)查看当前目录下的文件 pwd 查看目录的路径 who an i 查看当前用户 clear 清除屏幕 2.绝对路径:从根目录开始\ 相对路径:上一层.下一层 ...
- 基于 Spring MVC 的开源测试用例管理系统以及开发自测的实践
早前公司领导提出让开发自测,测试么也做做开发.当然了,为了保证自测质量,测试用例仍需测试提供,所以为了提高开发自测的效率和质量,我们开发了捉虫记.捉虫记是一个完整的Spring MVC项目,现已开源, ...
- iOS标注和适配
很多项目一开始没有注意美术素材的规范,这在后期会引起混乱.假如有机会做一个新项目(旧项目会有自己的历史问题,一下子很难改过来),建议设计师和程序员一起坐下来.共同设立一套规范,之后共同遵守. 下面说说 ...
- c++标准库容器【转】
C++最原始的容器之一是数组.数组的特点有: 1.大小固定 2.单独存在的数组建立在栈上,作为对象成员存在的数组建立在堆上还是栈上则要看作为宿主对象是被建立在堆上还是栈上.栈空间是有限的,所以如果数组 ...
- Servlet(一)基础总结
一.Servlet概述 1.Java Servlet是基于Java的一种技术和标准,是独立于平台和协议,服务器端的java应用程序.与Applet相比.Applet运行在客户端,而Servlet运行在 ...
- Java 静态代理与动态代理
代理模式 设想你的项目依赖第三方,但是你需要对其接口做一些数据检验.性能数据记录.异常处理等,合适的方法就是使用设计模式里的代理模式. 代理模式是常用的java设计模式,代理类与委托类有同样的接口,代 ...
- 使用vs2015搭建Asp.net Core
准备工具 1.首先得安装vs2015 并且升级至 update3及以上 2.安装.net core sdk.附上官网下载地址 http://www.microsoft.com/net/down ...
- Python HTMLTestRunner生成网页自动化测试报告时中文编码报错UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6
1. 由于使用Python Selenium做网页自动化测试时,有截取网页上的中文信息保存到测试结果中,最终出现编码错误如下: File "D:/PycharmProjects/AutoTe ...
- HashMap 学习笔记
先摆上JDK1.8中HashMap的类注释:我翻译了一下 /** * Hash table based implementation of the <tt>Map</tt> i ...
- xmlplus 组件设计系列之二 - 按钮
除了图标以外,按钮也许是最简单的组件了,现在来看看如何定义按钮组件. 使用原生按钮组件 在 xmlplus 中,HTML 元素也以组件的方式存在.所以,你可以直接通过使用 button 标签或者 in ...