譬如读取/META-INF/app.properties文件
方法1
InputStream is = XXX.class.getResourceAsStream("/META-INF/app.properties");
方法2
String filePath = XXX.class.getClassLoader().getResource("/META-INF/app.properties").getPath();
File file = new File(filePath);
方法类
import java.io.File;
import java.io.InputStream;
public class ReadResource {
public static InputStream getInputStream(String path) {
return ReadResource.class.getResourceAsStream(path);
}
public static File getFile(String path) {
String filePath = ReadResource.class.getClassLoader().getResource(path).getPath();
return new File(filePath);
}
public static void main(String[] args) {
System.out.println(getFile("hello.txt").getPath());
}
}