我的项目结构如下:

/src/main/java/
/src/main/resources/
/src/test/java/
/src/test/resources/

我在/src/test/resources/test.csv中有一个文件,我想从/src/test/java/MyTest.java中的单元测试中加载该文件

我有个不能用的代码。它会提示“没有这样的文件或目录”。

BufferedReader br = new BufferedReader (new FileReader(test.csv))

我也试过这个

InputStream is = (InputStream) MyTest.class.getResourcesAsStream(test.csv))

这也行不通。它返回null。我正在使用Maven构建我的项目。


当前回答

Try:

InputStream is = MyTest.class.getResourceAsStream("/test.csv");

IIRC getResourceAsStream()默认是相对于类的包。

正如@Terran所指出的,不要忘记在文件名的开头添加/

其他回答

在Spring项目中尝试以下代码

ClassPathResource resource = new ClassPathResource("fileName");
InputStream inputStream = resource.getInputStream();

或者在非弹簧项目上

 ClassLoader classLoader = getClass().getClassLoader();
 File file = new File(classLoader.getResource("fileName").getFile());
 InputStream inputStream = new FileInputStream(file);

下面的类可用于从类路径加载资源,并在给定的filePath出现问题时接收适当的错误消息。

import java.io.InputStream;
import java.nio.file.NoSuchFileException;

public class ResourceLoader
{
    private String filePath;

    public ResourceLoader(String filePath)
    {
        this.filePath = filePath;

        if(filePath.startsWith("/"))
        {
            throw new IllegalArgumentException("Relative paths may not have a leading slash!");
        }
    }

    public InputStream getResource() throws NoSuchFileException
    {
        ClassLoader classLoader = this.getClass().getClassLoader();

        InputStream inputStream = classLoader.getResourceAsStream(filePath);

        if(inputStream == null)
        {
            throw new NoSuchFileException("Resource file not found. Note that the current directory is the source folder!");
        }

        return inputStream;
    }
}

现在我正在说明从maven创建的资源目录中读取字体的源代码,

可控硅/主/资源/ calibril.ttf

Font getCalibriLightFont(int fontSize){
    Font font = null;
    try{
        URL fontURL = OneMethod.class.getResource("/calibril.ttf");
        InputStream fontStream = fontURL.openStream();
        font = new Font(Font.createFont(Font.TRUETYPE_FONT, fontStream).getFamily(), Font.PLAIN, fontSize);
        fontStream.close();
    }catch(IOException | FontFormatException ief){
        font = new Font("Arial", Font.PLAIN, fontSize);
        ief.printStackTrace();
    }   
    return font;
}

它为我工作,希望整个源代码也将帮助你,享受!

我让它工作,没有任何引用“类”或“ClassLoader”。

假设我们有三个文件位置的场景。你的工作目录(应用程序执行的地方)是home/mydocuments/program/projects/myapp:

a)工作目录的子文件夹后代: myapp / res /文件/ example.file

b)不属于工作目录的子文件夹: 项目/文件/ example.file

b2)另一个子文件夹不是工作目录的后代: 程序/文件/ example.file

c)根文件夹: 家庭/期间/文件/例子。文件(Linux;在Windows中将home/替换为C:)

1)选择正确的道路: a)字符串路径= "res/files/example.file"; b)字符串路径= "../projects/files/example.file" b2)字符串路径= "../../program/files/example.file" c)字符串路径= "/home/mydocuments/files/example.file"

基本上,如果它是根文件夹,则以斜杠开头。 如果是子文件夹,路径名前不能有斜杠。如果子文件夹不是工作目录的后代,你必须使用“../”cd到它。这告诉系统去一个文件夹。

2)通过传递正确的路径创建File对象:

File file = new File(path);

3)你现在可以开始了:

BufferedReader br = new BufferedReader(new FileReader(file));

这里有一个使用番石榴的快速解决方案:

import com.google.common.base.Charsets;
import com.google.common.io.Resources;

public String readResource(final String fileName, Charset charset) throws IOException {
        return Resources.toString(Resources.getResource(fileName), charset);
}

用法:

String fixture = this.readResource("filename.txt", Charsets.UTF_8)