Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.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
在JavaEclipse中复制文件?_Java_Eclipse_Path_Relative Path - Fatal编程技术网

在JavaEclipse中复制文件?

在JavaEclipse中复制文件?,java,eclipse,path,relative-path,Java,Eclipse,Path,Relative Path,我将一个名为templates的文件放在eclipse项目的src/文件夹中。我正试图复制该文件,并想对其进行编辑。这是我的密码: InputStream source = getClass().getClassLoader().getResourceAsStream("templates/file.docx"); File dest = new File("templates/updatedfile.docx"); try{ Files.copy(source, dest.toPat

我将一个名为templates的文件放在eclipse项目的src/文件夹中。我正试图复制该文件,并想对其进行编辑。这是我的密码:

InputStream source = getClass().getClassLoader().getResourceAsStream("templates/file.docx");
File dest = new File("templates/updatedfile.docx");

try{
    Files.copy(source, dest.toPath());
}
catch(Exception e)
{
    e.printStackTrace();
}
但是,我得到了一个java.nio.file.NoSuchFileException:template/updatedfile.docx!我做错了什么

编辑:很抱歉,修复了代码中的一个输入错误,确切的异常现在已更新

您可以使用:

System.out.printlnSystem.getPropertyuser.dir

查看默认情况下java查看的根文件夹的位置,它可能是您的项目文件夹。/src文件夹位于项目文件夹下,因此您可能也必须将其添加到路径中:


src/templates/updatedfile.docx

在eclipse中运行应用程序时,它会在项目文件夹中运行。因此,当您创建一个新文件templates/updatedfile.docx时,您试图同时创建一个新的目录和文件。 请尝试在项目文件夹中的creatign templates目录中重新运行该应用程序。
或者,您可以在代码中检查目录是否存在,如果不存在,则创建它

此文件templates/file.docx似乎未被识别为应用程序中的资源。确保此文件位于源代码中。否则,我建议改为使用具体路径。@反斜杠这是NullPointerException的通用Q/a。这种情况是一个相当特殊的问题,在我看来,API应该抛出一个IOException,因为在getClass.getClassLoader.getResourceAsStream中找不到该文件。您究竟从哪里获得该异常?@jny在我按照回答中的建议进行更改后,异常返回到nullpointerexception!user.dir返回/Users/username/eclipse/eclipse.app/Contents/MacOS/,完成建议的更改后,返回到nullpointerexception.and您的src文件夹在user.dir返回的内容下?不,它在my documents文件夹中!!确切地当您放置像/templates/updatedfile.docx或src/templates/updatedfile.docx这样的路径时,java假定它存在于user.dir返回的文件夹下。如果没有,请移动文件或放置绝对路径,如C:\Documents\…\。。etcI理解,但我希望这个应用程序是可移植的。我能做什么?对不起,如果我不清楚,我已经有了模板/目录-只需要复制那里的文件!嗯,它不应该在您的src目录中。只需在项目根目录中创建templates目录,并将源文件放在那里。您需要更改源文件路径的代码。但是,如果您希望保持当前的状态,则需要将dest file的路径更改为bin/templates/。。。。这就是你的应用程序读取源文件的位置。我尝试将所有内容都保存在根文件夹中,以使用输入模板/file.docx和输出模板/updatedfile.docx的相对路径,但没有成功!我得到一个java.nio.file.nosuchfileexception模板/file.docx