从项目文件夹java获取文件

时间:2013-06-25 00:52:31

标签: java file

我想通过使用“文件类”从项目文件夹中获取文件,我该怎么做?

File file=new File("x1.txt");

9 个答案:

答案 0 :(得分:21)

嗯,有许多不同的方法可以用Java获取文件,但这是一般的要点。

不要忘记你至少需要在try {} catch (Exception e){}内包装它,因为File是java.io的一部分,这意味着它必须有try-catch块。

不要踩到Ericson的问题,但是如果你使用的是实际的软件包,那么除非你明确使用它的位置,否则你会遇到文件位置的问题。相对路径与包裹混淆。

src/
    main.java
    x.txt

在此示例中,在File f = new File("x.txt");内使用main.java将引发文件未找到的异常。

但是,使用File f = new File("src/x.txt");会有效。

希望有所帮助!

答案 1 :(得分:11)

这听起来好像文件已嵌入到您的应用程序中。

您应该使用getClass().getResource("/path/to/your/resource.txt"),它会返回URLgetClass().getResourceAsStream("/path/to/your/resource.txt");

如果它不是嵌入式资源,那么您需要知道从应用程序的执行上下文到文件所在位置的相对路径

答案 2 :(得分:9)

如果您没有指定任何路径并只放置文件(就像您所做的那样),默认目录始终是您的项目之一(它不在" src&#中) 34;文件夹。它就在你项目的文件夹里面。)

答案 3 :(得分:7)

如果您尝试加载的文件与Java类不在同一目录中,则必须使用运行时目录结构而不是设计时出现的结构。

要了解运行时目录结构是什么,请检查{root project dir} / target / classes目录。该目录可通过"。" URL。

根据user4284592的回答,以下内容对我有用:

ClassLoader cl = getClass().getClassLoader();
File file = new File(cl.getResource("./docs/doc.pdf").getFile());

具有以下目录结构:

{root dir}/target/classes/docs/doc.pdf

这是一个解释,所以你不要盲目地复制和粘贴我的代码:

  • java.lang.ClassLoader是一个负责加载类的对象。每个Class对象都包含对定义它的ClassLoader的引用,并且可以使用getClassLoader()方法获取它。
  • ClassLoader' getResource方法查找具有给定名称的资源。资源是一些数据,可以通过类代码以独立于代码位置的方式访问。

答案 4 :(得分:2)

这些行在我的案例中起作用,

ClassLoader classLoader = getClass().getClassLoader();
File fi = new File(classLoader.getResource("test.txt").getFile());

在src

中提供了test.txt文件

答案 5 :(得分:0)

在javaFx 8中

javafx.scene.image.Image img = new javafx.scene.image.Image("/myPic.jpg", true);

URL url = new URL(img.impl_getUrl());

答案 6 :(得分:0)

application.yaml

中指定文件test/resources
ll src/test/resources/
total 6
drwxrwx--- 1 root vboxsf 4096 Oct  6 12:23 ./
drwxrwx--- 1 root vboxsf    0 Sep 29 17:05 ../
-rwxrwx--- 1 root vboxsf  142 Sep 22 23:59 application.properties*
-rwxrwx--- 1 root vboxsf   78 Oct  6 12:23 application.yaml*
-rwxrwx--- 1 root vboxsf    0 Sep 22 17:31 db.properties*
-rwxrwx--- 1 root vboxsf  618 Sep 22 23:54 log4j2.json*

从测试环境中,我可以使用

获取文件
String file = getClass().getClassLoader().getResource("application.yaml").getPath(); 

实际上会指向test-classes

中的文件
ll target/test-classes/
total 10
drwxrwx--- 1 root vboxsf 4096 Oct  6 18:49 ./
drwxrwx--- 1 root vboxsf 4096 Oct  6 18:32 ../
-rwxrwx--- 1 root vboxsf  142 Oct  6 17:35 application.properties*
-rwxrwx--- 1 root vboxsf   78 Oct  6 17:35 application.yaml*
drwxrwx--- 1 root vboxsf    0 Oct  6 18:50 com/
-rwxrwx--- 1 root vboxsf    0 Oct  6 17:35 db.properties*
-rwxrwx--- 1 root vboxsf  618 Oct  6 17:35 log4j2.json*

答案 7 :(得分:-1)

刚刚进行了快速谷歌搜索并找到了

Person* get_person() {
    std::string name = "Bob";
    return new Person(name);
}

int main (int argc, char **argv) {
    Person* person = get_person();
    // Is person's name Bob here? Or did Bob go out of scope?
    delete person;
}

将当前工作目录作为String返回。因此,要获取文件,请使用

System.getProperty("user.dir");

答案 8 :(得分:-1)

String path = System.getProperty("user.dir")+"/config.xml";
File f=new File(path);