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. SSH程序框架的整合(1)

    spring整合hibernate 有两种方式 1.注解方式 2.xml方式实现 Spring整合Hibernate有什么好处? 1.由IOC容器来管理Hibernate的SessionFactory ...

  2. 性能调优之MYSQL高并发优化下

    三.算法的优化 尽量避免使用游标,因为游标的效率较差,如果游标操作的数据超过1万行,那么就应该考虑改写..使用基于游标的方法或临时表方法之前,应先寻找基于集的解决方案来解决问题,基于集的方法通常更有效 ...

  3. Java并发编程:如何创建进程?

    转载自:http://www.cnblogs.com/dolphin0520/p/3913517.html 在前面一篇文章中已经讲述了在进程和线程的由来,今天就来讲一下在Java中如何创建线程,让线程 ...

  4. DataTable源码分析(二)

    DataTable源码分析(二) ===================== DataTable函数分析 ---------------- DataTable作为整个插件的入口,完成了整个表格的数据初 ...

  5. 学习Sass笔记之概念篇

    1 什么是CSS预处理器 首先我们了解一下什么是CSS预处理器:通俗的说,“CSS 预处理器用一种专门的编程语言,进行 Web 页面样式设计,然后再编译成正常的 CSS 文件,以供项目使用.CSS 预 ...

  6. 百度Web前端面试经历

    今天面了百度的前端实习职位.一面.时间大概是50分钟.面试官是位很帅气的小伙子,非常友好的一个人.进门的时候他让我等一会,我瞄了一眼他的电脑屏幕,发现他在coding…… 9点50开始的面试. 面试官 ...

  7. 关于binary log一点总结[转]

    阅读目录(Content) 1 what's binary log 2 Binary Logging Options and Variables 2.1 基础参数 3 Binary Logging F ...

  8. Keepalived高可用集群实践

    (1)实践的硬件环境准备 准备4台物理服务器或者4台VM虚拟机,其中两台用来做Keepalived服务器,两台做web测试站点 HOSTNAME I  P 解    释 lb01 10.0.0.7 K ...

  9. 基础SELECT示例掌握

    SELECT查询语句 ---进行单条记录.多条记录.单表.多表.子查询-- SELECT [ALL | DISTINCT | DISTINCTROW ] [HIGH_PRIORITY] [MAX_ST ...

  10. lua 模块

    lua 模块 概述 lua 模块类似于封装库 将相应功能封装为一个模块, 可以按照面向对象中的类定义去理解和使用 使用 模块文件示例程序 mod = {} mod.constant = "模 ...