导航:首页 > 编程语言 > javasrc路径怎么写

javasrc路径怎么写

发布时间:2023-03-05 08:52:39

java web工程中读取properties文件,路径一直不知道怎么写

1. 使用java.lang.Class类的getResourceAsStream(String name)方法

InputStreamin=getClass().getResourceAsStream("/config.properties");

在静态方法中,由于不能使用getClass()方法,必须写出类的名字。区别不大。

MyClass.class.getResourceAsStream("/config.properties");

使用这个方法,路径前面可以加斜杠也可以不加。根据Class类getResourceAsStream()方法的JavaDoc:

Finds a resource with a given name. The rules for searching resources associated with a given class are implemented by the defining class loader of the class. This method delegates to this object's class loader. If this object was loaded by the bootstrap class loader, the method delegates to ClassLoader.getSystemResourceAsStream.

Before delegation, an absolute resource name is constructed from the given resource name using this algorithm:

If the name begins with a '/' ('u002f'), then the absolute name of the resource is the portion of the name following the '/'.

Otherwise, the absolute name is of the following form:

modified_package_name/name

Where the modified_package_name is the package name of this object with '/' substituted for '.' ('u002e').

就是说,这个path假如以斜杠开头,则斜杠后面的部分是文件相对classpath的路径;假如不是,Java会把这个path看作是“包名/文件名”的结构,会尝试在这个类的包里面去找,而不是从classpath开始找;在这种情况下,除非你把properties文件放到MyClass.class所属的包里面,不然都会是null的。

2. 使用java.lang.ClassLoader类的getResourceAsStream(String name)方法


路径是不能加斜杠的!非常重要。

MyClass.class.getClassLoader().getResourceAsStream("config.properties");

这是因为使用classloader进行读取,所输入的参数必须是一个相对classpath的绝对路径,在格式上,一个绝对路径是不能以'/'开头的。

注意这两个方法是同名的,但路径参数的格式截然不同。


3. 在Maven中的运用

现在几乎所有的web project都是maven project,Maven的默认设置是把

src/main/resources/

加入到classpath里面的。那么,最好的做法是把你的properties文件放进src/main/resources里面,然后用上面代码读取。用Class类的,一般要加斜杠;用ClassLoader类的,绝不能加斜杠!

假如是Eclipse里面,需要把这个src/main/resources加到classpath里面。具体操作是右击工程,选择“Configure buildpath”,根据Maven的要求,把src/main/java和src/main/resources都加进去,并且保证Exclude是none,Include是all,或者至少要包括你需要读取的文件。

❷ java 获取src下的文件路径怎么写

java工程还是web工程?
java的话/src/就可以了啊
web的话,可以使用request.getServletContext().getRealPath("当前就是src下,这里可以写以后的路径");

❸ java获取某个文件夹的路径怎么写

File类有两个常用方法可以得到文件路径一个是:getCanonicalPath(),另一个是:getAbsolutePath(),可以通过File类的实例调用这两个方法例如file.getAbsolutePath()其中file是File的实例对象。下面是一个具体例子:
public class PathTest
{
public static void main(String[] args)
{
File file = new File(".\\src\\");
System.out.println(file.getAbsolutePath());
try
{
System.out.println(file.getCanonicalPath());
} catch (IOException e)
{
e.printStackTrace();
}
}
}
getAbsolutePath()和getCanonicalPath()的不同之处在于,getCanonicalPath()得到的是一个规范的路径,而getAbsolutePath()是用构造File对象的路径+当前工作目录。例如在上面的例子中.(点号)代表当前目录。getCanonicalPath()就会把它解析为当前目录但是getAbsolutePath()会把它解析成为目录名字(目录名字是点号)。
下面是上面程序在我电脑上的输出:
G:\xhuoj\konw\.\src\
G:\xhuoj\konw\src\

❹ java在linux下操作文件路径怎么写

一般文件路径在windows中用 \ 表示,但是在其他系统平台下比如linux中就不是 \ 所以java给我们提供了一个与平台无关的表示路径的常量 File.separator在windows中则表示 \ 比如现在有一个文件在D:\java\src\myjava中, 如何用绝对路径访问呢?
现在建立一个目录:
File fDir=new File(File.separator); //File.separator表示根目录,比如现在就表示在D盘下。
String strFile="java"+File.separator+"src"+File.separator+"myjava"; //这个就是绝对路径
File f=new File(fDir,strFile);

阅读全文

与javasrc路径怎么写相关的资料

热点内容
用户管理系统php模板 浏览:414
中国银行app账号怎么登录 浏览:862
安卓手机icloud云服务器地址 浏览:796
加密门禁如何写到小米手机 浏览:164
java静态方法的多线程 浏览:260
CAXAXp下载打不开要加密 浏览:239
开锁pdf 浏览:84
python可视化奶茶 浏览:219
命令方字幕 浏览:591
苹果手机如何连接ipad服务器 浏览:976
打印矩阵java 浏览:394
如何双开一个不能双开的app 浏览:529
抖音喜欢加密了怎么打开 浏览:386
mysql命令行显示表 浏览:364
国防离我们源码200字 浏览:778
vmwaretoolslinux 浏览:95
命令方块生存放不了吗 浏览:700
程序员入门很难吗 浏览:103
70后程序员照片 浏览:194
一个普通程序员能干多久 浏览:936