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. UILabel的讲解

    首先,我先自定义几个名词,方便接下来的讲解工作.如下图所示: 接下来,通过五个方面来讲解我们能对UILabel做出哪些改变或者称之为设置: 1.文字 1.1普通文字:内容text.字体大小font.字 ...

  2. java 基础知识五 数组

    java  基础知识五  数组 数组保存的是一组有顺序的.具有相同类型的数据. 同一个数组中所有数据元素的数据类型都是相同的. 可以通过数组下标来访问数组,数据元素根据下标的顺序,在内存中按顺序存放 ...

  3. WebForm捆绑压缩js和css(WebForm Bundling and Minification)

    .net framework 4以上,可以使用Microsoft.AspNet.Web.Optimization 新建4.0项目 Nuget搜索optimization,安装第一个包 加入Bundle ...

  4. jQuery的工作原理

    jQuery是为了改变javascript的编码方式而设计的. jQuery本身并不是UI组件库或其他的一般AJAX类库. 那么它是如何实现它的声明的呢? 先看一段简短的使用流程: (1).查找(创建 ...

  5. 解决Json传输中文乱码问题

    1.如果是通过URL传递:----需要编码两次 var searchText = this.searchText(); searchText = encodeURI(searchText); sear ...

  6. DateTime.Now的一些用法

    System.DateTime.Now.ToString("D");   //Tuesday, December 13, 2016 System.DateTime.Now.ToSt ...

  7. 基于Maven的SSM整合的web工程

    此文章主要有以下几个知识点: 一.如何创建 Maven的Web 工程 二.整合SSM(Spring,SpringMvc,Mybatis),包括所有的配置文件 三.用 mybatis 逆向工程生成对应的 ...

  8. 【原】Unity Shader VS UDK Material Editor

    UDK 的材质编辑器十分好用,毕竟是所见即所得的.虽然unity也有类似第三方插件,但易用性还是差很多,下面主要是,把一些常见表达式概念对应起来. 1. UDK CameraVector (相机位向量 ...

  9. 不须组件的NPOI插件 excel读取

    前提: 需要DLL  1.引用 using NPOI.SS.UserModel; using NPOI.XSSF.UserModel;//用于2007版本 using NPOI.HSSF.UserMo ...

  10. Java多线程的几个常用关键字

    一.同步(synchronized)和异步(asynchronized) 1.同步(synchronized)简单说可以理解为共享的意思,如果资源不是共享的,就没必要进行同步.设置共享资源为同步的话, ...