java src 同级目录在src目录下怎么创建平行包?如图

第3课 初识Java web项目结构(二)_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
第3课 初识Java web项目结构(二)
阅读已结束,下载文档到电脑
想免费下载更多文档?
定制HR最喜欢的简历
下载文档到电脑,方便使用
还剩4页未读,继续阅读
定制HR最喜欢的简历
你可能喜欢Maven的web项目里面如何在java类里面直接获取src/main/resources下面的文件对象?
例如src/main/resources目录下面有个config/interfaces/quick-stock.xml文件
我想直接在某一个Java类里面获取到这个文件的File对象,怎么整?(注意噢:是获取File对象。。。)
xxx.class.getResource("/configs/interface/....");
xxx.class.getResourceAsStream("/xxx/xx/x/x/x/x/x")
--- 共有 2 条评论 ---
new File(xxx.class.getResource("/aa/bb/cc.xml"));
我想获取的是File对象
String filePath = XXX.class.getClassLoader().getResource("/configs/interface/..").getPath();
File file = new File(filePath);
String filePath = XXX.class.getClassLoader().getResource("/configs/interface/..").getPath();
File file = new File(filePath);
类似下面的错
java.nio.file.InvalidPathException: Illegal char &:& at index 2: /D:/56qq/codes/usercenter/usercenter-server/target/test-classes/data/test.jpg
&& &at sun.nio.fs.WindowsPathParser.normalize(WindowsPathParser.java:182)
&& &at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:153)
File f = new File(this.getClass().getResource("/data/test.jpg").getPath());
(f.getAbsolutePath()); // windows和macbook都是正常的
URL resource = getClass().getClassLoader().getResource("data/test.jpg");
(resource.getPath()); // windows下会出现"/"开头
Path path = Paths.get(f.getAbsolutePath());
byte[] data = Files.readAllBytes(path);
这种方式是可以的。
使用File来包一次。Java加载src目录下文件的几种方式
package com.future.
import java.io.FileNotFoundE
import java.io.IOE
import java.io.InputS
import java.util.P
@SuppressWarnings("serial")
&&& public class
CommonPropertiesUtil {
@SuppressWarnings("static-access")
&&& public
static String getContextPath(){
String contextPath="";
&&&&&&&&&&&&
&&&&&&&&&&&
//加载src目录下的文件的几种方式
&&&&&&&&&&&
&&&&&&&&&&&
//注意当使用getClass()方式而不是getClassLoader()时资源文件前的"/"不能省略
&&&&&&&&&&&
&&&&&&&&&&&
//InputStream
stream=this.getClass().getResourceAsStream("/common.properties");
&&&&&&&&&&&
&&&&&&&&&&&
//InputStream
stream=Thread.currentThread().getClass().getResourceAsStream("/common.properties");&&&&&&&&&&&
&&&&&&&&&&&&
InputStream
stream=Thread.currentThread().getContextClassLoader().getResourceAsStream("common.properties");
&&&&&&&&&&&
&&&&&&&&&&&
//谨记这种方式是错误的
&&&&&&&&&&&
//InputStream
stream=Thread.currentThread().getClass().getClassLoader().getResourceAsStream("common.properties");
&&&&&&&&&&&
&&&&&&&&&&&
//InputStream
stream=Thread.currentThread().getClass().getClassLoader().getSystemResourceAsStream("common.properties");
&&&&&&&&&&&
&&&&&&&&&&&
//InputStream
stream=this.getClass().getClassLoader().getSystemResourceAsStream("common.properties");
&&&&&&&&&&&
&&&&&&&&&&&
//InputStream
stream=this.getClass().getClassLoader().getResourceAsStream("common.properties");
&&&&&&&&&&&
&&&&&&&&&&&
//InputStream
stream=this.getClass().getClassLoader().getSystemResourceAsStream("common.properties");
&&&&&&&&&&&
&&&&&&&&&&&
//InputStream
stream=CommonPropertiesUtil.class.getClassLoader().getResourceAsStream("common.properties");&&&&&&&&&&&
&&&&&&&&&&&
//InputStream
stream=CommonPropertiesUtil.class.getClass().getClassLoader().getSystemClassLoader().getResourceAsStream("common.properties");
&&&&&&&&&&&
&&&&&&&&&&&
//InputStream
stream=CommonPropertiesUtil.class.getClass().getClassLoader().getSystemClassLoader().getSystemResourceAsStream("common.properties");
&&&&&&&&&&&
&&&&&&&&&&&&
Properties properties=new Properties();
&&&&&&&&&&&&
properties.load(stream);
&&&&&&&&&&&
&&&&&&&&&&&&
contextPath=properties.getProperty("contextPath");
&&&&&&&&&&&
&&&&&&&&&&&&
System.out.println(contextPath);
} catch (FileNotFoundException e) {
&&&&&&&&&&&
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
return contextP
&&& public
static void main(String[] args) {
getContextPath();
注:当直接使用getClass()而不是getClassLoader()方法时括号中资源文件名称前的/不能省略
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。}

我要回帖

更多关于 java获取src同级目录 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信