java 从当前包中获取文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7893274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 21:51:59  来源:igfitidea点击:

Getting a file from current package

javafilepath

提问by Himalay Majumdar

This code runs well in my locally but gives file not found exception when we build using bamboo. Any idea/workaround?

这段代码在我的本地运行良好,但是当我们使用竹子构建时会出现文件未找到的异常。任何想法/解决方法?

final static String FILE_NAME ="/src/test/java/com/statement/SamplePDFStatementFile.txt";
file = new File(FILE_NAME);
FileInputStream fis = new FileInputStream(file);

I basically want to test a class which reads a file. Here is the Complete Code.

我基本上想测试一个读取文件的类。这是完整的代码。

//Main Class
public class PdfRenderer {

    public void render(PdfFile pdfFile) throws IOException {
        FileInputStream fis = new FileInputStream(pdfFile.getFile());

        final HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
        response.setHeader("Content-Disposition", " attachment; filename=" + pdfFile.getAttachmentName());

        byte[] buff = new byte[2048];
        int bytesRead;
        while (-1 != (bytesRead = fis.read(buff, 0, buff.length))) {
            response.getOutputStream().write(buff, 0, bytesRead);
        }

        response.getOutputStream().flush();

    }
}

//Test

public class PdfRendererTest{

    PdfFile pdfFile;
    File file;

    @Test
    public void test_DetailStatementsByAccountNumber() throws Exception {
        new AbstractSeamTest.ComponentTest() {

            PdfRenderer actionBean = new PdfRenderer();

            URL url = this.getClass().getResource("/SamplePDFStatementFile.txt");
            final String FILE_NAME = url.getFile();

            protected void testComponents() throws Exception {

                file = new File(FILE_NAME);

                context.checking(new Expectations() {{
                    one(pdfFile).getFile();
                    will(returnValue(file));
                    one(pdfFile).getAttachmentName();
                    will(returnValue(file.getName()));
                }});

                actionBean.render(pdfFile);
            }

        }.run();
    }
}

I want to set an expectation that pdfFile.getFile() returns SamplePDFStatementFile.txt. If I use getResourceAsStream, am not sure how to convert it to a File Object.

我想设置 pdfFile.getFile() 返回 SamplePDFStatementFile.txt 的期望。如果我使用 getResourceAsStream,我不确定如何将其转换为文件对象。

OK.. so now I am using. Looks like thats the answer :)

好的..所以现在我正在使用。看起来这就是答案:)

public void inputStreamToFile() throws Exception{
                InputStream inputStream =  this.getClass().getResourceAsStream("/SamplePDFStatementFile.txt");

                file = File.createTempFile("abc","def");
                OutputStream out = new FileOutputStream(file);

                int length = 0;
                byte[] bytes = new byte[1024];

                while ((length = inputStream.read(bytes)) != -1) {
                       out.write(bytes, 0, length);
                }

                inputStream.close();
                out.flush();
                out.close();
            }

回答by axtavt

There are no reliable ways to reference files relative to the project root folder.

没有可靠的方法来引用与项目根文件夹相关的文件。

You need to reference this file as a resource instead. As far as I see, you use Maven. If so, you need to put this file into /src/test/resourcesrather than /src/test/java(perhaps you can also configure Maven to get resources from /src/test/java, but it would be a violation of Maven directory layout convention).

您需要将此文件作为资源引用。据我所知,您使用 Maven。如果是这样,你需要把这个文件放入/src/test/resources而不是/src/test/java(也许你也可以配置Maven从 中获取资源/src/test/java,但这会违反Maven目录布局约定)。

After that you can load this file as

之后,您可以将此文件加载为

InputStream fis = getClass()
    .getResourceAsStream("/com/statement/SamplePDFStatementFile.txt");

Or, if the current class is in the com.statementpackage, as

或者,如果当前类在com.statement包中,如

InputStream fis = getClass().getResourceAsStream("SamplePDFStatementFile.txt");