0

I've a folder with name "test" and has many folders and files inside it. I've to copy this folder to a dynamic location in Java. Below is the code I've written:

class FileManager {
    public static void copy(Path path) throws IOException {
        InputStream inputStream = FileManager.class.getClassLoader().getResourceAsStream("test");
        Files.copy(inputStream, path, StandardCopyOption.REPLACE_EXISTING);
    }
}

This is copying to the location but not copying as a folder, but as a file. The path string I'm sending is "C:/Users/test". Most importantly I want this code working in jar. I cannot get the physical location of the "test" folder in the jar.

User1230321
  • 1,435
  • 4
  • 23
  • 39
  • There is no simple way to do this, because there is no actual folder named "test". Jar files are zip files and do not have directories, just a sequence of entry names that happen to look like hierarchical file names. See https://stackoverflow.com/questions/56188427/how-to-copy-folders-out-of-resources-from-jar-both-in-runtime-and-dev-env/56189875. – VGR Jul 10 '19 at 21:26
  • You can't list the resources inside a Jar file that easy. The common way is not to include multiple files into the resources, but one ZIP file that contains all files you want to extract. Using `ZipInputStream` you can then extract those files to the target directory. – Robert Jul 11 '19 at 07:27

0 Answers0