我试图读取在CLASSPATH系统变量中设置的文本文件。不是用户变量。

我试图获得输入流文件如下:

将文件目录(D:\myDir)放在CLASSPATH中,然后尝试如下操作:

InputStream in = this.getClass().getClassLoader().getResourceAsStream("SomeTextFile.txt");
InputStream in = this.getClass().getClassLoader().getResourceAsStream("/SomeTextFile.txt");
InputStream in = this.getClass().getClassLoader().getResourceAsStream("//SomeTextFile.txt");

将文件(D:\myDir\SomeTextFile.txt)的完整路径放在CLASSPATH中,并尝试上面的3行代码。

但不幸的是,他们都没有工作,我总是得到null到我的输入流。


当前回答

我使用webshpere应用服务器和我的Web模块是建立在Spring MVC。测试。属性位于资源文件夹中,我尝试使用以下方法加载此文件:

.getResourceAsStream .getClassLoader this.getClass()()(“Test.properties”); this.getClass () .getResourceAsStream (" / Test.properties ");

上面的代码都没有加载文件。

但是在下面代码的帮助下,属性文件被成功加载:

.getResourceAsStream .getContextClassLoader Thread.currentThread()()(“Test.properties”);

感谢用户“user1695166”。

其他回答

请尝试

InputStream in = this.getClass().getResourceAsStream("/SomeTextFile.txt");

您的尝试没有成功,因为只有类的类装入器能够从类路径装入。您为java系统本身使用了类装入器。

你必须把你的“系统变量”放在Java类路径上。

通过类路径上的目录,从同一个类加载器加载的类中,你应该能够使用以下任何一种:

// From ClassLoader, all paths are "absolute" already - there's no context
// from which they could be relative. Therefore you don't need a leading slash.
InputStream in = this.getClass().getClassLoader()
                                .getResourceAsStream("SomeTextFile.txt");
// From Class, the path is relative to the package of the class unless
// you include a leading slash, so if you don't want to use the current
// package, include a slash like this:
InputStream in = this.getClass().getResourceAsStream("/SomeTextFile.txt");

如果这些都不起作用,那就说明有其他问题。

举个例子,这段代码:

package dummy;

import java.io.*;

public class Test
{
    public static void main(String[] args)
    {
        InputStream stream = Test.class.getResourceAsStream("/SomeTextFile.txt");
        System.out.println(stream != null);
        stream = Test.class.getClassLoader().getResourceAsStream("SomeTextFile.txt");
        System.out.println(stream != null);
    }
}

这个目录结构是:

code
    dummy
          Test.class
txt
    SomeTextFile.txt

然后(使用Unix路径分隔符,因为我在Linux机器上):

java -classpath code:txt dummy.Test

结果:

true
true

当使用Spring框架时(作为实用工具或容器的集合—您不需要使用后一种功能),您可以轻松地使用Resource抽象。

Resource resource = new ClassPathResource("com/example/Foo.class");

通过Resource接口,您可以以InputStream、URL、URI或File的形式访问资源。将资源类型更改为文件系统资源,只需更改实例即可。

我的回答与问题中所问的不完全一样。相反,我给出了一个解决方案,我们可以很容易地从我们的项目类路径读取文件到java应用程序。

例如,假设配置文件example.xml位于如下路径

com.myproject.config.dev

我们的Java可执行类文件在下面的路径:-

com.myproject.server.main

now just check in both the above path which is the nearest common directory/folder from where you can access both dev and main directory/folder (com.myproject.server.main - where our application’s java executable class is existed) – We can see that it is myproject folder/directory which is the nearest common directory/folder from where we can access our example.xml file. Therefore from a java executable class resides in folder/directory main we have to go back two steps like ../../ to access myproject. Now following this, see how we can read the file:-

package com.myproject.server.main;

class Example {

  File xmlFile;

  public Example(){
       String filePath = this.getClass().getResource("../../config/dev/example.xml").getPath();
       this.xmlFile = new File(filePath);
    }

  public File getXMLFile() {
      return this.xmlFile;
  }
   public static void main(String args[]){
      Example ex = new Example();
      File xmlFile = ex.getXMLFile();
   }
}