2

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

4

3 に答える 3

4

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.

于 2012-04-23T15:03:35.370 に答える
2

use "getResourceAsStream"

-> http://mindprod.com/jgloss/getresourceasstream.html

于 2012-04-23T15:04:56.867 に答える
1

given a resource that you want to write to a given Path path, then you can use:

try(InputStream is = this.getClass().getResourceAsStream(resource)){
    Files.copy(is, path);
} catch (Exception e){
    throw new RuntimeException(e);
}
于 2017-05-07T11:56:33.700 に答える