通过java获取当前项目路径

通过java获取当前项目路径,第1张

使用url统一资源定位符,前提是需要有访问文件的权限。

import javaioFile;

import javanetURL;

import orgapachecommonsioFileUtils;

public class DownloadURLFile {

/

  @param args

 /

public static void main(String[] args) {

String res = downloadFromUrl(">

互联网项目一般会有单独的服务器存放静态资源,就是一种静态资源,在这里就是区别于项目部署的另一台服务器。这时候你项目里面都是使用相对路径,像你上面所说的常量/opt/upload/这种做法是正确的,上传的时候,常见的有使用日期分目录存储的,如/opt/upload/2014/11/03/jpg,对于的路径,数据库里一般来说保存到2014/11/03/jpg就可以了。这是存。

取,一般来说资源都必须发布服务才能让外网访问。例如,你可以在你项目中写个servlet用来读取,如下面的服务地址>

最常用读取properties文件的方法

InputStream in = getClass()getResourceAsStream("资源Name");这种方式要求properties文件和当前类在同一文件夹下面。如果在不同的包中,必须使用:

InputStream ins = thisgetClass()getResourceAsStream("/cn/zhao/properties/testPropertiesPath2properties");

Java中获取路径方法

获取路径的一个简单实现

反射方式获取properties文件的三种方式

1 反射方式获取properties文件最常用方法以及思考:

Java读取properties文件的方法比较多,网上最多的文章是"Java读取properties文件的六种方法",但在Java应用中,最常用还是通过javalangClass类的getResourceAsStream(String name) 方法来实现,但我见到众多读取properties文件的代码中,都会这么干:

InputStream in = getClass()getResourceAsStream("资源Name");

这里面有个问题,就是getClass()调用的时候默认省略了this!我们都知道,this是不能在static(静态)方法或者static块中使用的,原因是static类型的方法或者代码块是属于类本身的,不属于某个对象,而this本身就代表当前对象,而静态方法或者块调用的时候是不用初始化对象的。

问题是:假如我不想让某个类有对象,那么我会将此类的默认构造方法设为私有,当然也不会写别的共有的构造方法。并且我这个类是工具类,都是静态的方法和变量,我要在静态块或者静态方法中获取properties文件,这个方法就行不通了。

那怎么办呢?其实这个类就不是这么用的,他仅仅是需要获取一个Class对象就可以了,那还不容易啊--

取所有类的父类Object,用Objectclass难道不比你的用你正在写类自身方便安全吗 ?呵呵,下面给出一个例子,以方便交流。

import javautilProperties;

import javaioInputStream;

import javaioIOException;

/

读取Properties文件的例子

File: TestPropertiesjava

User: leizhimin

Date: 2008-2-15 18:38:40

/

public final class TestProperties {

private static String param1;

private static String param2;

static {

Properties prop = new Properties();

InputStream in = Object class getResourceAsStream( "/testproperties" );

try {

propload(in);

param1 = propgetProperty( "initYears1" )trim();

param2 = propgetProperty( "initYears2" )trim();

} catch (IOException e) {

eprintStackTrace();

}

}

/

私有构造方法,不需要创建对象

/

private TestProperties() {

}

public static String getParam1() {

return param1;

}

public static String getParam2() {

return param2;

}

public static void main(String args[]){

Systemoutprintln(getParam1());

Systemoutprintln(getParam2());

}

}

运行结果:

151

152

当然,把Objectclass换成intclass照样行,呵呵,大家可以试试。

另外,如果是static方法或块中读取Properties文件,还有一种最保险的方法,就是这个类的本身名字来直接获取Class对象,比如本例中可写成TestPropertiesclass,这样做是最保险的方法

2 获取路径的方式:

File fileB = new File( this getClass()getResource( "" )getPath());

System out println( "fileB path: " + fileB);

22获取当前类所在的工程名:

System out println("userdir path: " + System getProperty ("userdir"))<span style="background-color: white;">3 获取路径的一个简单的Java实现</span>

/

获取项目的相对路径下文件的绝对路径

@param parentDir

目标文件的父目录,例如说,工程的目录下,有lib与bin和conf目录,那么程序运行于lib or

bin,那么需要的配置文件却是conf里面,则需要找到该配置文件的绝对路径

@param fileName

文件名

@return一个绝对路径

/

public static String getPath(String parentDir, String fileName) {

String path = null;

String userdir = SystemgetProperty("userdir");

String userdirName = new File(userdir)getName();

if (userdirNameequalsIgnoreCase("lib")

|| userdirNameequalsIgnoreCase("bin")) {

File newf = new File(userdir);

File newp = new File(newfgetParent());

if (fileNametrim()equals("")) {

path = newpgetPath() + Fileseparator + parentDir;

} else {

path = newpgetPath() + Fileseparator + parentDir

+ Fileseparator + fileName;

}

} else {

if (fileNametrim()equals("")) {

path = userdir + Fileseparator + parentDir;

} else {

path = userdir + Fileseparator + parentDir + Fileseparator

+ fileName;

}

}

return path;

}

4 利用反射的方式获取路径:

InputStream ips1 = Enumeration class getClassLoader() getResourceAsStream( "cn/zhao/enumStudy/testPropertiesPath1properties" );

InputStream ips2 = Enumeration class getResourceAsStream( "testPropertiesPath1properties" );

InputStream ips3 = Enumeration class getResourceAsStream( "properties/testPropertiesPath2properties" );

mapxml没加路径吧 或是和你的java执行文件不在一个目录或classpath里

你参考下

public final class InitHibernate {

private static String configFile = "/rmmshibernatexml";

private Configuration config;

public void init(){

config=new Configuration()configure(configFile);

configsetProperties(HibernateUtilgetProperties());

}

public SessionFactory buildSessionFactory(){

return configbuildSessionFactory();

}

public Configuration getHibernateConf(){

return config;

}

}

public class DAOConfig {

private static Logger logger = LogManagergetLogger(DAOConfigclass);

private static final String DAO_CONFIG_FILE = "daoxml";

private static final String DAO_CONFIG_SECTION = "DAO";

public static synchronized HashMap load() {

HashMap<Object, Object> map = new HashMap<Object, Object>();

JFigLocator jfigLocator = new JFigLocator(DAO_CONFIG_FILE);

JFigIF daoConfig = JFiggetInstance(jfigLocator);

Properties prop = daoConfiggetSectionAsProperties(DAO_CONFIG_SECTION);

Enumeration enumSection = propkeys();

while (enumSectionhasMoreElements()) {

String daoIface = (String) enumSectionnextElement();

String daoImpl = propgetProperty(daoIface);

try {

Class iface = ClassToolkitloadClass(daoIface);

Class impl = ClassToolkitloadClass(daoImpl);

// 将接口作为HashMap索引,实现类作为值

mapput(iface, implgetConstructor()newInstance());

} catch (ClassNotFoundException e) {

loggerdebug("No Class Found =>" + e);

} catch (Exception ex) {

loggerdebug("create instance failed =>" + ex);

}

}

return map;

}

}

使用 javaioFile对象

File[]

listFiles(FileFilter filter)

Returns an array of abstract pathnames denoting the files and

directories in the directory denoted by this abstract pathname that

satisfy the specified filter

以上就是关于通过java获取当前项目路径全部的内容,包括:通过java获取当前项目路径、如何使用java自动读取web项目中的excel文件、java中如何从当前项目中读取运行另一项目中的配置文件等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9589055.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-29
下一篇2023-04-29

发表评论

登录后才能评论

评论列表(0条)

    保存