Java的文件路径或文件位置 - new file() [英] File path or file location for Java - new file()

查看:4307
本文介绍了Java的文件路径或文件位置 - new file()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <$> $ p 
$ b

在我的项目中有以下结构。 c $ c> myPorjectName
src
com.example.myproject
a.java
com.example.myproject.data
b.xml
code>

a.java 中,我想读 b.xml 文件。我怎样才能做到这一点?具体来说,在 a.java 中,我使用了下面的代码:

  DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); 
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse(new File(data / b.xml));

这段代码找不到 b.xml 。但是,如果我将路径更改为 src / com / example / myproject / data / b.xml ,那么它可以工作。目前的位置似乎在我的项目文件的根。



但是我看到其他人的例子, if b.xml a .java 在同一个文件夹中,那么我们可以直接使用 new File(b.xml)。把 b.xml 放在 a.java 的文件夹中,而不是放在子文件夹中, strong>不起作用。如果这个工作,那么在我的情况下,我应该能够使用新的文件(data / b.xml),对不对?我真的不明白为什么这是行不通的。

解决方案

如果它已经在类路径中,并在相同的包中,使用

  URL url = getClass()。getResource(b.xml); 
File file = new File(url.getPath());

或者,读取它作为 InputStream

  InputStream input = getClass()。getResourceAsStream(b.xml); 

静态方法中,可以使用

  InputStream in = YourClass.class.getResourceAsStream(b.xml); 

如果您的文件与您尝试访问该文件的类不在同一个包中,那么你必须给它的相对路径从'/'开始。

  ex:InputStream input = getClass()。getResourceAsStream 
(/resources/somex.cfg.xml\");这是另一个jar资源文件夹中的


I have the following structure for my project.

In Eclipse:

myPorjectName
  src
    com.example.myproject
        a.java
    com.example.myproject.data
        b.xml

In a.java, I want to read b.xml file. How can I do that? Specifically, in a.java, I used the following code:

DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse (new File("data/b.xml"));

This code cannot find b.xml. However, if I change the path to src/com/example/myproject/data/b.xml then it works. The current location seems to be in the root of my project file.

But I see other people's examples, if b.xml and a.java are in the same folder, then we can directly use new File("b.xml"). But I try putting b.xml in the same folder of a.java rather than putting in the sub folder, but it still does not work. If this works, then in my case, I should be able to use new File("data/b.xml"), right? I really do not understand why this is not working.

解决方案

If it is already in the classpath and in the same package, use

URL url = getClass().getResource("b.xml");
File file = new File(url.getPath());

OR , read it as an InputStream:

InputStream input = getClass().getResourceAsStream("b.xml");

Inside a static method, you can use

InputStream in = YourClass.class.getResourceAsStream("b.xml");

If your file is not in the same package as the class you are trying to access the file from, then you have to give it relative path starting with '/'.

ex : InputStream input = getClass().getResourceAsStream
           ("/resources/somex.cfg.xml");which is in another jar resource folder

这篇关于Java的文件路径或文件位置 - new file()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆