gpt4 book ai didi

java - 如何将资源文件夹从 jar 复制到程序文件中

转载 作者:行者123 更新时间:2023-12-01 18:16:14 28 4
gpt4 key购买 nike

我花了几个小时寻找答案,但我就是想不通,我正在尝试复制我的资源文件夹,其中包含我正在运行的游戏的所有图像和数据文件 jar 并放入E:/Program Files/mtd/当我从 eclipse 中运行它时它工作正常,但是当我导出 jar 并尝试它时,我得到 NoSuchFileException

`JAR
Installing...
file:///C:/Users/Cam/Desktop/mtd.jar/resources to file:///E:/Program%20Files/mtd
/resources
java.nio.file.NoSuchFileException: C:\Users\Cam\Desktop\mtd.jar\resources
at sun.nio.fs.WindowsException.translateToIOException(Unknown Source)
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.WindowsFileAttributeViews$Basic.readAttributes(Unknown Sou
rce)
at sun.nio.fs.WindowsFileAttributeViews$Basic.readAttributes(Unknown Sou
rce)
at sun.nio.fs.WindowsFileSystemProvider.readAttributes(Unknown Source)
at java.nio.file.Files.readAttributes(Unknown Source)
at java.nio.file.FileTreeWalker.walk(Unknown Source)
at java.nio.file.FileTreeWalker.walk(Unknown Source)
at java.nio.file.Files.walkFileTree(Unknown Source)
at java.nio.file.Files.walkFileTree(Unknown Source)
at me.Zacx.mtd.main.Game.<init>(Game.java:94)
at me.Zacx.mtd.main.Game.main(Game.java:301)`

这是我正在使用的代码:

    if (!pfFolder.exists()) {
pfFolder.mkdir();
try {

URL url = getClass().getResource("/resources/");
URI uri = null;

if (url.getProtocol().equals("jar")) {
System.out.println("JAR");
JarURLConnection connect = (JarURLConnection) url.openConnection();
uri = new URI(connect.getJarFileURL().toURI().toString() + "/resources/");

} else if (url.getProtocol().equals("file")) {
System.out.println("FILE");
uri = url.toURI();
}

final Path src = Paths.get(uri);
final Path tar = Paths.get(System.getenv("ProgramFiles") + "/mtd/resources/");

System.out.println("Installing...");
System.out.println(src.toUri() + " to " + tar.toUri());

Files.walkFileTree(src, new SimpleFileVisitor<Path>() {
public FileVisitResult visitFile( Path file, BasicFileAttributes attrs ) throws IOException {
return copy(file);
}
public FileVisitResult preVisitDirectory( Path dir, BasicFileAttributes attrs ) throws IOException {
return copy(dir);
}
private FileVisitResult copy( Path fileOrDir ) throws IOException {
System.out.println("Copying " + fileOrDir.toUri() + " to " + tar.resolve( src.relativize( fileOrDir ) ).toUri());
Files.copy( fileOrDir, tar.resolve( src.relativize( fileOrDir ) ) );
return FileVisitResult.CONTINUE;
}
});
System.out.println("Done!");
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
}

最佳答案

这比我想象的要难,但这里是如何做到的。

这是我的复制方法引用https://examples.javacodegeeks.com/core-java/io/file/4-ways-to-copy-file-in-java/

public void copyFile(String inputPath, String outputPath ) throws IOException
{

InputStream inputStream = null;

OutputStream outputStream = null;
try {

inputStream = getClass().getResourceAsStream(inputPath);
outputStream = new FileOutputStream(outputPath);

byte[] buf = new byte[1024];

int bytesRead;

while ((bytesRead = inputStream.read(buf)) > 0) {

outputStream.write(buf, 0, bytesRead);

}

}
finally {


inputStream.close();

outputStream.close();

}

请注意此图中 Jar 文件的项目结构 Project structure

现在我需要读取 Jar 文件。这是此解决方案的变体 How can I get a resource "Folder" from inside my jar File? 。这两种方法一起工作以产生结果。我已经测试过这个并且它有效。

公共(public)类主要{

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub

final String pathPartOne = "test/com";
final String pathPartTwo = "/MyResources";
String pathName = "C:\\Users\\Jonathan\\Desktop\\test.jar";

JarTest test = new JarTest();

final File jarFile = new File(pathName);

if(jarFile.isFile()) { // Run with JAR file
final JarFile jar = new JarFile(jarFile);
final Enumeration<JarEntry> entries = jar.entries(); //gives ALL entries in jar
while(entries.hasMoreElements()) {
final String name = entries.nextElement().getName();

if (name.startsWith(pathPartOne+pathPartTwo + "/")) { //filter according to the path
if(name.contains("."))//has extension
{
String relavtivePath = name.substring(pathPartOne.length()+1);
String fileName = name.substring(name.lastIndexOf('/')+1);
System.out.println(relavtivePath);
System.out.println(fileName);
test.copyFile(relavtivePath, "C:\\Users\\Jonathan\\Desktop\\" + fileName);
}

}
}
jar.close();
}


}

}

希望有帮助。

关于java - 如何将资源文件夹从 jar 复制到程序文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36238451/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com