我在使用URI类时遇到了麻烦。
我可以在c:\中创建一个zip文件,代码如下:
// Properties for archive file we're creating
Map<String, String> archiveProperties = new HashMap<>();
archiveProperties.put("create", "true");
archiveProperties.put("encoding", "UTF-8");
URI archiveLocation = URI.create("jar:file:/" + "my.zip");
// Create archive
FileSystem archive = FileSystems.newFileSystem(archiveLocation, archiveProperties);
但我真的希望从目前的工作目录中创建一个目录,如果你愿意的话,创建一个目录。我尝试了很多东西,包括:
// Properties for archive file we're creating
Map<String, String> archiveProperties = new HashMap<>();
archiveProperties.put("create", "true");
archiveProperties.put("encoding", "UTF-8");
URI archiveLocation = URI.create("jar:file:../" + "my.zip");
// Create archive
FileSystem archive = FileSystems.newFileSystem(archiveLocation, archiveProperties);
但我得到一个例外,在这种情况下URI不是分层的,或者它继续在c:\
中创建答案 0 :(得分:1)
我终于提出了一个解决方案,虽然不是很漂亮:
// Properties for archive file we're creating
Map<String, String> archiveProperties = new HashMap<>();
archiveProperties.put("create", "true");
archiveProperties.put("encoding", "UTF-8");
String filePathName = System.getProperty("user.dir") + FILE_SEPARATOR + ".." + FILE_SEPARATOR + "myfile.zip";
filePathName = filePathName.replace('\\','/');
filePathName = filePathName.replaceAll(" ", "%20");
URI archiveLocation = URI.create("jar:file:///" + filePathName);
// Create archive
FileSystem archive = FileSystems.newFileSystem(archiveLocation, archiveProperties);
注意FILE_SEPARATOR来自System.getProperty(file.separator)
答案 1 :(得分:0)
快速修复将使用此
URI archiveLocation = URI.create("jar:file:///"+new File(".").getAbsolutePath()+"/../my.zip");