简体   繁体   English

如何在Maven中从src / main / resources复制文件?

[英]How do I copy a file from src/main/resources in Maven?

imgur

Hi, I want to copy "play button.png" to "C:\\Users\\Wyatt\\AppData\\Roaming\\.The Labyrinth\\Assets\\Images". 嗨,我想将“播放button.png”复制到“ C:\\ Users \\ Wyatt \\ AppData \\ Roaming \\ .The Labyrinth \\ Assets \\ Images”。 I tried useing this code: 我尝试使用此代码:

File appdata = new File(System.getenv("APPDATA"));
File datafolder = new File(appdata, ".The Labyrinth");

File assets = new File(datafolder, "Assets");
assets.mkdir();

Files.copy(Paths.get("src\\main\\resources\\assets\\play button.png"), Paths.get(assets + "\\Images\\play button.png"));

But it throws an exception. 但这引发了异常。

java.nio.file.NoSuchFileException: src\main\resources\assets\play button.png -> C:\Users\Wyatt\AppData\Roaming\.The Labyrinth\Assets\Images\play button.png
at sun.nio.fs.WindowsException.translateToIOException(Unknown Source)
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.WindowsFileCopy.copy(Unknown Source)
at sun.nio.fs.WindowsFileSystemProvider.copy(Unknown Source)
at java.nio.file.Files.copy(Unknown Source)
at com.awsp8.labyrinth.TheLabyrinth.install(TheLabyrinth.java:73)
at com.awsp8.labyrinth.TheLabyrinth.main(TheLabyrinth.java:32)

TheLabyrinth.java:73 is this code: TheLabyrinth.java:73是以下代码:

Files.copy(Paths.get("src\\main\\resources\\assets\\play button.png"), Paths.get(assets + "\\Images\\play button.png"));

Maybe I'm doing this wrong? 也许我做错了吗? I dont know. 我不知道。 Thanks in advance. 提前致谢。

Not sure what are you trying to do, but: 不知道您要做什么,但是:

1) In your java code use / instead of \\ , it will work also on windows and it is better to maintain. 1)在您的Java代码中,使用/代替\\,它也可以在Windows上运行,并且最好进行维护。

2) Your src/main/resources/assets/play button.png is relative to destination where you run your code and I doubt that C:\\Users\\Wyatt\\AppData\\Roaming.The Labyrinth\\Assets\\Images\\ is the directory you want to point. 2)您的src / main / resources / assets / play button.png相对于您运行代码的目标位置,我怀疑C:\\ Users \\ Wyatt \\ AppData \\ Roaming.Labyrinth \\ Assets \\ Images \\是您的目录要点。

3) If you have your play button.png (terrible name, I hate names with spaces :-)) in \\main\\reources and use maven standard layout you probably have it on your classpath, don' t you ? 3)如果在\\ main \\ reources中有您的play button.png(糟糕的名字,我讨厌带有空格的名称:-))并使用Maven标准布局,则可能在类路径中有它,不是吗? If so, why not use something like ClassLoader.getSystemClassLoader().getResourceAsStream("play button.png") to obtain InputStream to read from ? 如果是这样,为什么不使用类似ClassLoader.getSystemClassLoader()。getResourceAsStream(“ play button.png”)之类的方法来获取要从中读取的InputStream呢?

4) if you really want to copy something via Maven (which your question caption is about, but i am pretty sure your question not) this is the sample how to do it: 4)如果您真的想通过Maven复制某些内容(您的问题标题与之有关,但是我很确定您的问题不是),这是示例操作方法:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>${maven.antrun.plugin.version}</version>
    <executions>
        <execution>
            <id>copy jars</id>
            <phase>install</phase>
            <configuration>
                <target>
                    <copy file="${project.basedir}/src/main/resources/play button.png"
                                tofile="somewhere i want to copy.png" />
                </target>
                </configuration>
                <goals>
                    <goal>run</goal>
                </goals>
        </execution>
    </executions>
</plugin>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不使用 src/main/resources 的情况下在资源文件夹中使用 GSON 编写 JSON 文件? - How do I write a JSON file using GSON in the resources folder without using src/main/resources? 从Maven项目资源文件夹(src / main / resources)加载excel(.xlsx / .xls)文件 - Loading an excel(.xlsx/.xls) file from maven project resources folder (src/main/resources) Java:如何在src / main / resources中的test中打开文本文件? - Java: How do I open a text file in test that is in src/main/resources? 从src / main / resources中读取文件作为文件? - Reading file from src/main/resources as File? 如何使用注释处理器从 src/main/resources 读取文件? - How to read file from src/main/resources with annotation processor? 如何在JUnit测试中从src / main / resources中读取文件? - How to read file from src/main/resources relative in a JUnit test? 从/ src / main / resources加载文件 - Loading file from /src/main/resources 从 /src/main/resources/ 读取文件 - Read file from /src/main/resources/ 如何在src / main / resources中创建文件 - How to create a file in src/main/resources 使用maven构建后找不到来自src / main / resources的资源 - Resource from src/main/resources not found after building with maven
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM