I want to create Java program that creates certain HTML files and since they all contain some images, program should开发者_高级运维 also copy those images to the user.dir, where HTML files are created. I have those images in package "resources", code is in package "code". How do I make that happend?
Basically, you will need a list of the resource files that you wish to copy. Them you use
public class CopyUtil {
public void doTheCopy( List<String> resourceNames ) {
for ( String resource : resourceNames ) {
InputStream is = this.getClass().getClassLoader().getResourceAsStream(resource);
FileOutputStream fos =
new FileOutputStream( new File(System.getProperty("user.dir"), resource));
byte[] buffer = new byte[1024];
int read = -1;
while( (read = is.read(buffer)) != -1 ) {
fos.write( buffer,0,read);
}
fos.flush();
fos.close();
}
}
}
精彩评论