Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
包中的文件,不从java应用程序打开_Java_File_Pdf_File Io - Fatal编程技术网

包中的文件,不从java应用程序打开

包中的文件,不从java应用程序打开,java,file,pdf,file-io,Java,File,Pdf,File Io,亲爱的专家们,这个问题与 更新的问题 我使用了以下两个代码: 如果我从工作区运行应用程序,下面的代码可以正常工作 URL resource = Thread.currentThread().getContextClassLoader().getResource("resources/User_Guide.pdf"); File userGuideFile = null; try { userG

亲爱的专家们,这个问题与

更新的问题

我使用了以下两个代码:

如果我从
工作区运行应用程序
,下面的代码可以正常工作

URL resource = Thread.currentThread().getContextClassLoader().getResource("resources/User_Guide.pdf");  
            File userGuideFile = null;  
            try {  
                userGuideFile = new File(resource.getPath());  
                if (Desktop.isDesktopSupported())  
                {  
                    Desktop desktop = Desktop.getDesktop();  
                    desktop.open(userGuideFile);  
                }  
            } catch (Exception e1) {  
                e1.printStackTrace();  
            }  
但如果我将我的
project.jar
复制到另一个位置,它将不会打开文件并在我的日志中显示为
文件未找到“c:\workspace\project…pdf”
。我使用了以下代码,我的pdfReaderadobe reader显示异常
文件不受支持或损坏

代码:

if (Desktop.isDesktopSupported())     
{     
    Desktop desktop = Desktop.getDesktop();     
    InputStream resource = Thread.currentThread().getContextClassLoader().getResource("resources/User_Guide.pdf");  
try  
{  
    File file = File.createTempFile("User_Guide", ".pdf");  
    file.deleteOnExit();  
    OutputStream out = new FileOutputStream(file);  
    try  
    {  
        // copy contents from resource to out  
    }  
    finally  
    {  
        out.close();  
    }  
    desktop.open(file);     
}     
finally  
{  
    resource.close();  
}  
}  
请告诉我一些想法。谢谢你的帮助。多谢各位


注意:我试图打开
*.txt
文件,它工作正常。但不能在
PDF
DOC
中工作。主要问题是当我运行应用程序更改项目工作空间目录时。实际上,我想制作这样的文件:Ntebeans键盘短代码文档,它位于
帮助
菜单下

jar是一个zip存档文件。因此,首先使用7zip/WinZip或类似工具查看它。 检查其中的路径是否确实是
resources/User\u Guide.pdf
(区分大小写!)。 它很可能是jar中的
/User\u Guide.pdf

无法立即从资源中获取文件(即文件系统上的文件)(只是偶然)。这样就得到了一个
InputStream

InputStream in = getClass().getResource("/resources/User_Guide.pdf");
NullPointerException
未找到时。使用getClass时,类必须位于同一个jar中,本例中的路径以
/
开头

现在,您可以将输入流复制到某个临时文件,并打开该文件。在Java 7中:

File file = File.createTempFile("User_Guide", ".pdf");  
Files.copy(in, file.toPath());
如果在Files.copy行中出现FileAlreadyExistsException,请添加以下CopyOption:

Files.copy(in, file.toPath(), StandardCopyOption.REPLACE_EXISTING);

对于java我没有足够的声誉来评论Joop Eggen的公认答案,但是

InputStream in = getClass().getResource("/resources/User_Guide.pdf");
抛出错误“不兼容类型:URL无法转换为InputStream”。而是使用getResourceAsStream()方法:


同一工作区内的另一个位置或不同计算机的不同工作区同一台计算机的另一个位置,例如:
我的工作区在E:\workspace
内,我将我的jar文件复制到
D:\apps
。我必须将我的应用程序分发到50多台计算机上…新路径是否包含
空格
?@AdeelAnsari没有空格包中没有文件或文件。只有资源。资源有URL,URL有流,但URL不是文件名。你在这里使用了错误的技术。仅自Java7以来存在。对于以前的版本,如果您不想重新发明轮子,则存在ApacheCommons io类来执行此类任务。
InputStream in = getClass().getResource("/resources/User_Guide.pdf");
InputStream in = getClass().getResourceAsStream("/resources/User_Guide.pdf");