Java listFiles in directory in jar

前端 未结 1 715
说谎
说谎 2020-12-11 22:08

Is there any way to use listFiles() on a directory that\'s been packaged into a jar?

Let\'s say I have a directory in my resource directory with some te

相关标签:
1条回答
  • 2020-12-11 22:32

    No. java.io.File can only be used to list real directories.

    However, you can treat it as a java.nio.file.Path.

    Overall, you have three options:

    1. Open the .jar as a Zip File System and use Files.newDirectoryStream or Files.list.
    2. Iterate through all entries in the .jar file, looking for names that match.
    3. Put a text file in your .jar that contains the names of all the entries in the directory, so you don't have to try to list them.
    0 讨论(0)
提交回复
热议问题