开发者

How to make program copy file from package to user.dir in java?

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();
    }
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜