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的更多相关文章

  1. springMVC+mybatis 进行单元测试时 main SqlSessionFactoryBean - Parsed configuration file: 'class path resource' 无限的读取xml文件

    今天终于写完的Dao层的操作,怀着无比激动的心情,进行单元测试,就在最后一个方法,对的就是最后一个方法,启动单元测试就会报以下错误: [2016-05-11 18:25:01,691] [WARN ] ...

  2. 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 ...

  3. Spring源码分析——资源访问利器Resource之实现类分析

    今天来分析Spring的资源接口Resource的各个实现类.关于它的接口和抽象类,参见上一篇博文——Spring源码分析——资源访问利器Resource之接口和抽象类分析 一.文件系统资源 File ...

  4. eclipse中 linked resource的使用

    一.关于linked resource   eclipse 中的linkded resources 是指存放在项目所在位置以外某个地方的文件或者文件夹:这些特定的资源必须有一个项目作为他们的父资源.l ...

  5. java Resource

    ClassPathResource: String resource = ""; //相对路径 Resource resource = new ClassPathResource( ...

  6. 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 ...

  7. 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 ...

  8. 查找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 ...

  9. Spring Resource之内置的Resource实现

    Spring提供了大量的并且可以直接使用的Resource实现 1.UrlResource UrlResource封装了一个java.net.URL,而且可以通过一个URL用于访问任何对象,例如文件. ...

随机推荐

  1. 结合ThreadLocal来看spring事务源码,感受下清泉般的洗涤!

    在我的博客spring事务源码解析中,提到了一个很关键的点:将connection绑定到当前线程来保证这个线程中的数据库操作用的是同一个connection.但是没有细致的讲到如何绑定,以及为什么这么 ...

  2. 使用react native制作的一款网络音乐播放器

    使用react native制作的一款网络音乐播放器 基于第三方库 react-native-video设计"react-native-video": "^1.0.0&q ...

  3. iOS实现高斯模糊效果(Swift版本)

    给UIimage添加分类 extension UIImage { /// 高斯模糊 func gaussianBlur(var blurAmount:CGFloat) -> UIImage { ...

  4. Azure WAF防火墙工作原理分析和配置向导

    Azure WAF工作原理分析和配置向导 本文博客地址为:http://www.cnblogs.com/taosha/p/6716434.html ,转载请保留出处,多谢! 本地数据中心往云端迁移的的 ...

  5. position relative

    position的默认值是static,(也就是说对于任意一个元素,如果没有定义它的position属性,那么它的position:static) 如果你想让这个#demo里的一个div#sub相对于 ...

  6. iOS 让View始终在屏幕最上层

    UIView层次管理 放到最上层 放到最下层 将一个UIView显示在最前面只需要调用其父视图的 bringSubviewToFront()方法. 将一个UIView层推送到背后只需要调用其父视图的  ...

  7. Java程序i学习中各阶段的建议

    第一部分:对于尚未做过Java工作的同学,包括一些在校生以及刚准备转行Java的同学. 一.Java基础 首先去找一个Java的基础教程学一下,这里可以推荐一个地址,或者你也可以参照这个地址上去找相应 ...

  8. CSS3 & SVG 制作钟表

    在线演示      源码下载

  9. Linux中Nginx反向代理下的tomcat集群

    Nginx具有反向代理(注意和正向代码的区别)和负载均衡等特点. 这次Nginx安装在 192.168.1.108 这台linux 机器上.安装Nginx 先要装openssl库,gcc,PCRE,z ...

  10. 蚂蚁金服新一代数据可视化引擎 G2

    新公司已经呆了一个多月,目前着手一个数据可视化的项目,数据可视化肯定要用到图形库如D3.Highcharts.ECharts.Chart等,经决定我的这个项目用阿里旗下蚂蚁金服所开发的G2图表库. 官 ...