【问题标题】:Getting a file from current package从当前包中获取文件
【发布时间】:2011-10-25 17:25:56
【问题描述】:

此代码在我的本地运行良好,但在我们使用竹子构建时出现文件未找到异常。有什么想法/解决方法吗?

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

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

//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();
    }
}

我想设定 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();
            }

【问题讨论】:

  • 当你 build 或 run 它时它会给出异常?
  • 了解如何使用类加载器(上下文类加载器,或加载当前类的类)中的 getResource 方法,使用从根包 (com) 开始的路径。您似乎使用的是绝对路径。
  • 看起来文件在 SRC 文件夹下,通常构建排除 SRC 文件夹,你能检查一下吗?

标签: java file path


【解决方案1】:

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

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

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

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

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

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

【讨论】:

  • 根据您的建议,我已将文本文件放在src/test/resources/SamplePDFStatementFile.txt 和我的主类中,即src/test/java/com/billmelater/csa/action/statement/PdfRendererTest.java 我做InputStream fis = getClass().getResourceAsStream("SamplePDFStatementFile.txt"); 但它返回null
  • @Himalay:由于你的class的包和资源的文件夹不对应,所以需要使用完整路径,如下:getResourceAsStream("/SamplePDFStatementFile.txt")。
  • 谢谢 axtavt.. 你让我开心了.. 最后一个澄清是如果你觉得下面的代码没问题(执行明智)。不想再次破坏构建。 URL url = this.getClass().getResource("/SamplePDFStatementFile.txt");final String FILE_NAME = url.getFile();file = new File(FILE_NAME);FileInputStream fis = new FileInputStream(pdfFile.getFile());
  • @Himalay:这段代码看起来很奇怪,如果您的应用程序被打包到jar 文件中,可能无法正常工作。 getResourceAsStream() 怎么了?
  • 基本上我正在寻找一个引用物理文件的 File 对象实例。文件文件---> SamplePDFStatementFile.txt
猜你喜欢
  • 1970-01-01
  • 2013-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-09
  • 1970-01-01
  • 1970-01-01
  • 2012-07-20
相关资源
最近更新 更多