java项目中读取src目录下的文件】的更多相关文章

private void getUser(String tmpfile){ Properties props = new Properties(); props.load(DbTask.class.getClassLoader().getResourceAsStream( tmpfile)); return props.getProperty("user"); } 方法说明: tmpfile是传入的需要读取的文件名 这个文件一定要跟DbTask类,在一个包下面 调用: getUser(…
注意两点: 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 │   │   │ …
这种情况遇见的会比较多,像一个WEB工程,如果在src下面写了一个xml或者一些其它的文件,当工程发布到服务器时,web程序是在tomcat等服务器下运行这个程序的,这个时候,程序目录里面并没有src文件夹,那么这种情况下要怎么办呢? 其实当程序发布到服务器时,src下面的文件也会被一起发布到服务器中,只不过是没有了src的目录,这种情况下,可以用如下代码来获取: /** * 数据文件的位置 */ private static String dbFilePath=""; /** * 获…
// // 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…
需要读取resources目录下的文件,那么方法如下: 假设在资源目录下的template目录下有一个文件a.txt,获取到文件流的方式 InputStream stream = this.getClass().getClassLoader().getResourceAsStream("template/a.txt"); 读取之后可以对这个流进行操作,如下载文件,具体的使用方法详见https://www.cnblogs.com/zys2019/p/12245606.html#_labe…
在Maven项目的开发中,当需要读取src/下的配置文件时,该怎么做? 我们假设Resources下有一个文件名为kafka.properties的配置文件(为什么用kafka.properties,因为这是在做kafka项目的时候碰到的问题,在网上查到了不少信息,索性当个搬运工,再根据自己的理解整理一下) 1.在java类中读取若配置文件不在src/main/resources目录下,可以直接使用 Properties prop = new properties(); prop.load(ne…
public GetResource{ String path = GetResource.class.getClassLoader().getResource("xx/xx.txt").getPath(); } ----------扩展 一 相对路径的获得 说明:相对路径(即不写明时候到底相对谁)均可通过以下方式获得(不论是一般的java项目还是web项目) String relativelyPath=System.getProperty("user.dir");…
1. 资源文件 2. 加载文件 public void test() { try { System.out.println("begin test"); String filepath = "log4j2.yml"; // 此处取项目路径 + 传入的路径,改路径获取不到文件 // 如果要获取文件需要传入 src/main/resources/log4j2.xml File temp = new File(filepath); System.out.println(t…
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,…