从当前包中获取文件

Him*_*dar 1 java file path

这个代码在我的本地运行良好但在使用bamboo构建时给出了文件未找到的异常.任何想法/解决方法?

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

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

//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();
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

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();
            }
Run Code Online (Sandbox Code Playgroud)

axt*_*avt 6

没有可靠的方法来引用相对于项目根文件夹的文件.

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

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

InputStream fis = getClass()
    .getResourceAsStream("/com/statement/SamplePDFStatementFile.txt");
Run Code Online (Sandbox Code Playgroud)

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

InputStream fis = getClass().getResourceAsStream("SamplePDFStatementFile.txt");
Run Code Online (Sandbox Code Playgroud)