将java.nio.file.Path转换为file got失败



我正在尝试将.cer文件打包在一个jar中,并使用java将它们动态安装到密钥库中。

private List<File> doSomething(Path p)  {

List<java.io.File>FileList=new ArrayList<>();
try {

int level = 1;
if (p.toString().equals("BOOT-INF/classes/certs")) {
level = 2;
Stream<Path> walk = Files.walk(p, level);
for (Iterator<Path> it = walk.iterator(); it.hasNext(); ) {
System.out.println(it.next());//getting all .cer files in the folder
FileList.add(it.next().toFile());//getting an error UnsupportedOperationException Path not associated with
}

logger.info("fileList" + FileList.size());
}
}
catch(Exception e)
{
logger.error("error-----------"+e);
}
return FileList;
}

我在toFile((中得到了UnsupportedOperationException,相信这是因为我试图访问jar中的文件。有没有办法将这个路径(nio.file.Path(转换为实际的文件或流?

Path#toString((不会像您要比较的字符串路径那样返回带有正斜杠(/(的路径。它会返回带有反斜杠(
\(的路径,因此您的比较永远不会变为真。

从本地文件系统读取路径时,始终使用与系统相关的路径或文件分隔符。要做到这一点,您可以使用String#replaceAll((方法将任何路径名斜杠转换为适用于代码操作的文件系统的斜杠,例如:

String pathToCompare = "BOOT-INF/classes/certs"
.replaceAll("[\/]", "\" + File.separator);

这应该很好:

private List<java.io.File> doSomething(java.nio.file.Path p) {
java.util.List<java.io.File> FileList = new java.util.ArrayList<>();
try {
int level = 1;
String pathToCompare = "BOOT-INF/classes/certs"
.replaceAll("[\/]", "\" + File.separator);
if (p.toString().equals(pathToCompare)) {
level = 2;
java.util.stream.Stream<Path> walk = java.nio.file.Files.walk(p, level);
for (java.util.Iterator<Path> it = walk.iterator(); it.hasNext();) {
System.out.println(it.next());//getting all .cer files in the folder
FileList.add(it.next().toFile());//getting an error UnsupportedOperationException Path not associated with
}
}
}
catch (Exception e) {
System.err.println(e);
}
return FileList;
}

编辑:

如果您想根据特定的文件扩展名和/或JAR文件的内容列出清单,那么您需要做一些不同的事情。下面的代码与您一直使用的代码非常相似,只是:

  • 方法名称更改为getFilesList((
  • 列表返回为List<String>,而不是List<File>
  • 深度级别现在是提供给方法的参数(始终确保深度级别足以执行任务(
  • 可选的String args参数(名为:onlyExtensions(已被添加到方法中,以便可以使用一个(或多个(文件扩展名应用于返回仅包含文件所在路径的列表名称包含应用的扩展名。如果提供了扩展恰好是".jar",则该JAR文件的内容也将将应用于返回的列表。如果什么都不提供,那么所有文件在列表中返回

对于JAR文件,还提供了一个辅助方法:

修改任何你认为合适的代码:

public static List<String> getFilesList(String thePath, int depthLevel, String... onlyExtensions) {
Path p = Paths.get(thePath);
java.util.List<String> FileList = new java.util.ArrayList<>();
try {
java.util.stream.Stream<Path> walk = java.nio.file.Files.walk(p, depthLevel);
for (java.util.Iterator<Path> it = walk.iterator(); it.hasNext();) {
File theFile = it.next().toFile();
if (onlyExtensions.length > 0) {
for (String ext : onlyExtensions) {
ext = ext.trim();
if (!ext.startsWith(".")) {
ext = "." + ext;
}
if (!theFile.isDirectory() && theFile.getName().substring(theFile.getName().lastIndexOf(".")).equalsIgnoreCase(ext)) {
FileList.add(theFile.getName() + " --> " + theFile.getAbsolutePath());
}
else if (!theFile.isDirectory() && theFile.getName().substring(theFile.getName().lastIndexOf(".")).equalsIgnoreCase(".jar")) {
List<String> jarList = getFilesNamesFromJAR(theFile.getAbsolutePath());
for (String strg : jarList) {
FileList.add(theFile.getName() + " --> " + strg);
}
}
}
}
else {
FileList.add(theFile.getAbsolutePath());
}
}
}
catch (Exception e) {
System.err.println(e);
}
return FileList;
}

JAR文件助手方法:

public static java.util.List<String> getFilesNamesFromJAR(String jarFilePath) {
java.util.List<String> fileNames = new java.util.ArrayList<>();
java.util.zip.ZipInputStream zip = null;
try {
zip = new java.util.zip.ZipInputStream(new java.io.FileInputStream(jarFilePath));
for (java.util.zip.ZipEntry entry = zip.getNextEntry(); entry != null; entry = zip.getNextEntry()) {
fileNames.add(entry.getName());
}
}
catch (java.io.FileNotFoundException ex) {
System.err.println(ex);
}
catch (java.io.IOException ex) {
System.err.println(ex);
}
finally {
try {
if (zip != null) {
zip.close();
}
}
catch (IOException ex) {
System.err.println(ex);
}
}
return fileNames;
}

要使用getFileList((方法,您可以执行以下操作:

List<String> fileNames = getFilesList("C:\MyDataFolder", 3, ".cer", ".jar");
// Display files in fileNames List
for (String str : fileNames) {
System.out.println(str);
}

最新更新