Search code examples
javafilewriter

Java How do I write a resource (internal text file) out to a directory?


I have a file I'm storing within my jar that I use a default setting file. I wish to write this file out to a user defined path. How do I write it out? This file that I'm trying to write out is in the same location as my class files that will be writing this file


Solution

  • Use getResourceAsStream to access the resource. Create a FileOutputStream for the file you wish to write. Read from one stream and write to the other. Preferably, use buffering, and don't forget to close your streams when you're done.

    See Location-Independent Access to Resources.