0

I'm working on my localhost on spring boot app, where I use references of some files. For example :

"src/main/resources/myZip.zip"

As I know, in production distribution package of this app will not contain /src folder.

Can someone please tell me another way how can I get different references of this files for producton?

Also can someone suggest how can I create new file in resources?

I am using

Files.write(Path.of("src/main/resources/newFile.zip), fileContent, StandardCharsets.UTF_8);

to add new file, but I'm interested in how to create new file without

"src/main/resources"

  • Kindly dont use `src/main/resources` in your paths..even for DEV/Test purposes. Please look into `getClass().getClassLoader().getResourceAsStream("folderDirectlyInsideResources/otherPackages/file.txt");` if you want an InputStream, or `getResource()` if you wanted a URL – JCompetence May 02 '22 at 07:54
  • Thank you. Changed with getClass().getClassLoader().getResource("myZip.zip"). If you know, I also use Files.readAllLines(Path.of("myZip.zip"), StandardCharsets.UTF_8) this Path.of needs String and not URL, how can I handle this? – Nikusha Ozashvili May 02 '22 at 08:52
  • You cant use Paths.of, do `Paths.get(getClass().getClassLoader().getResource("directoryUnderResources/file.txt").toURI());` instead and that gives you a PATH – JCompetence May 02 '22 at 08:55
  • Thank you so much. Could you also please tell me last thing?, how can I create new file in resources? I was using Files.write(Path.of("src/main/resources/newFile.zip), fileContent, StandardCharsets.UTF_8); but now its not working – Nikusha Ozashvili May 02 '22 at 09:48
  • https://stackoverflow.com/questions/36130111/java-write-txt-file-in-resource-folder You shouldnt. The idea is to create a file in your PC where the jar runs. You cant modify the contents of src/main/resources of the jar file itself.. – JCompetence May 02 '22 at 10:29

0 Answers0