![](/img/trans.png)
[英]How do I write a JSON file using GSON in the resources folder without using src/main/resources?
[英]How do I copy a file from src/main/resources in Maven?
嗨,我想將“播放button.png”復制到“ C:\\ Users \\ Wyatt \\ AppData \\ Roaming \\ .The Labyrinth \\ Assets \\ Images”。 我嘗試使用此代碼:
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"));
但這引發了異常。
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是以下代碼:
Files.copy(Paths.get("src\\main\\resources\\assets\\play button.png"), Paths.get(assets + "\\Images\\play button.png"));
也許我做錯了嗎? 我不知道。 提前致謝。
不知道您要做什么,但是:
1)在您的Java代碼中,使用/代替\\,它也可以在Windows上運行,並且最好進行維護。
2)您的src / main / resources / assets / play button.png相對於您運行代碼的目標位置,我懷疑C:\\ Users \\ Wyatt \\ AppData \\ Roaming.Labyrinth \\ Assets \\ Images \\是您的目錄要點。
3)如果在\\ main \\ reources中有您的play button.png(糟糕的名字,我討厭帶有空格的名稱:-))並使用Maven標准布局,則可能在類路徑中有它,不是嗎? 如果是這樣,為什么不使用類似ClassLoader.getSystemClassLoader()。getResourceAsStream(“ play button.png”)之類的方法來獲取要從中讀取的InputStream呢?
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.