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用于访问任何对象,例如文件. ...
随机推荐
- windows环境搭建jira 详解
一.事前准备 1:JDK下载并安装:http://www.oracle.com/technetwork/java/javase/downloads/index.html2:MySQL JDBC连接驱动 ...
- .NET Core 2.0及.NET Standard 2.0
.NET Core 2.0的发布时间,.NET Core 2.0预览版及.NET Standard 2.0 Preview大概在5月中旬或下旬发布. .NET Core 2.0正式版本发布时间大约在Q ...
- 深入Web请求过程
B/S网络架构 带来的好处: 1.客户端使用同一的浏览器. --浏览器的交互特性使其使用起来非常简便 2.服务器基于统一的http. --简化.规范开发模式,大大节省开发成本.如tomcat ngi ...
- ios 苹果手机硬件摘要
IPhone4 * 2010年发布. * A4单核处理器. * 3.5英寸Retina显示屏(视网膜屏幕),960x640像素分辨率. * 后置摄像头500万像素. * 前置摄像头30万像素. IPh ...
- C++模板--实现容器适配器
STL源码初接触 STL = Standard Template Library,直译过来是:标准模板库,是惠普实验室开发的一系列软件的统称.从根本上说,STL是一些"容器"的集合 ...
- 最近公共祖先LCA(Tarjan算法)的思考和算法实现
LCA 最近公共祖先 Tarjan(离线)算法的基本思路及其算法实现 小广告:METO CODE 安溪一中信息学在线评测系统(OJ) //由于这是第一篇博客..有点瑕疵...比如我把false写成了f ...
- [.NET产品源码保护].NET防止反编译(非混淆加密)
.NET产品源码保护产生的背景: .NET源码加密方案支持C#及VB.NET等语言开发的ASP.NET及WINFORM应用.利用.NET支持托管代码与非托管代码共存的特性,将C#代码经过处理放于非托管 ...
- CPP--借助神器VS理解内存存储
之前也有想了解这些,第一个不是学底层的不知道从何理解,第二个上网搜概念,大牛们三言两语就结束了,举得例子也比较复杂,对于非C方向的可能有点吃力,所以一直没理解. 今天偶然发现原来还要内存窗口之说,就慢 ...
- 开发Angular库的简单指导(译)
1. 最近工作上用到Angular,需要查阅一些英文资料,虽然英文非常烂,但是种种原因又不得不硬着头皮上,只是每次看英文都很费力,因此决定将一些比较重要的特别是需要反复阅读的资料翻译一下,以节约再次阅 ...
- js基本语法汇总
1.分类 ECMAScript js基本语法与标准 DOM Document Object Model文档对象模型 BOM Browser Object Model浏览器对象模型 tips:DOM和B ...