this.getClass().getResource("file path")
I know this will access the file in maven's resource folder.
But how can I create a new file in maven resource folder.
您不能简单地将类路径映射到URL或JAR文件,并且无法在现有的JAR或远程URL中“创建”资源。
If you know that the class file for the given class won't be loaded from a JAR file when its code executes, you can use something like this:
File file = new File(getClass().getResource(filename).toURI());
file.createNewFile();
This pattern can be useful in unit test code, which typically executes before any class files are packed into a jar.
Note that you may need to wrap this in a try-catch block, because the URL.toURI()
method may throw a URISyntaxException. However, for test code, you might just want to add a throws
clause to the method declaration.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.