正确读取resources目录下的文件】的更多相关文章

问题描述:本地可以正常读取areacode.json文件,打成jar包在测试环境找不到该文件. 问题代码: static { StringBuffer strbuffer = new StringBuffer(); try { String path = Thread.currentThread().getContextClassLoader().getResource("areacode.json").getPath(); File myFile = new File(path);…
注意两点: 1. 将资源目录添加到 build path,确保该目录下的文件被拷贝到 jar 文件中. 2. jar 内部的东西,可以当作 stream 来读取,但不应该当作 file 来读取. 例子 新建一个 maven 目录 App.java 用于读取 resources 中的 a.txt 内容. a.txt 是被读取的资源文件. grs@grs App $ tree . ├── pom.xml ├── src │   ├── main │   │   ├── java │   │   │ …
需要读取resources目录下的文件,那么方法如下: 假设在资源目录下的template目录下有一个文件a.txt,获取到文件流的方式 InputStream stream = this.getClass().getClassLoader().getResourceAsStream("template/a.txt"); 读取之后可以对这个流进行操作,如下载文件,具体的使用方法详见https://www.cnblogs.com/zys2019/p/12245606.html#_labe…
问题描述: Springboot没有打成jar之前,可以成功获取读取resources目录下xxx.json文件的 路径.但是打成jar包后,接口调不通,原因是获取不到文件的路径. 原因: 在本地进行调试时,文件是真实存在于磁盘的某个目录.此时通过获取文件路径,是可以正常读取的,因为文件确实存在. 而打包成jar以后,实际上文件是存在于jar这个文件里面的资源文件,在磁盘是没有真实路径的. 所以通过ResourceUtils.getFile或者this.getClass().getResourc…
// // main.m // 读取指定目录下的文件列表 // // Created by Apple on 15/11/24. // Copyright © 2015年 Apple. All rights reserved. // /* *读取指定目录下的文件列表 */ #import <Foundation/Foundation.h> void myQuickMethod(); int main(int argc, const char * argv[]) { //文件操作对象 NSFil…
读取某个目录下的文件,如'/Users/test/test_kmls'目录下有test1.txt.test2.txt. 第一种方法读出的all_files是test1.txt.test2.txt import os kml_path=os.path.abspath('/Users/test/test_kmls') all_files=os.listdir(kml_path) for file in all_files: print file 第二种方法可以获得文件的全路径,读出的all_file…
package gao.org; import java.io.FileNotFoundException; import java.io.IOException; import java.io.File; public class ReadFile { public ReadFile() { } /** * 读取某个目录下的全部文件 */ public static boolean readfile(String filepath) throws FileNotFoundException,…
背景:最近做项目重构将以前的ssh + angular js架构,重构为spring boot + vue.项目是一个数据管理平台,后台涉及到多表关联查询,数据导入导出等. 问题:读取resource 目录下文件时出现路径找不到. 原因:spring boot 将项目打包为jar,使用 java - jar 包名 在服务器上运行.此时文件为打包文件,所以不能通过路径获取到文件.类似不能读取压缩包中的文件,必须先解压缩.结论:spring boot 中的文件只能通过流来进行读取. 可以通过以下方法…
/** * @Author: * @Description:获取某个目录下所有直接下级文件,不包括目录下的子目录的下的文件,所以不用递归获取 * @Date: */ public static List<String> getFiles(String path) { List<String> files = new ArrayList<String>(); File file = new File(path); File[] tempList = file.listFi…
首先,看文档: Streaming Assets   Most assets in Unity are combined into the project when it is built. However, it is sometimes useful to place files into the normal filesystem on the target machine to make them accessible via a pathname. An example of this…