开发者

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

开发者 https://www.devze.com 2022-12-17 01:34 出处:网络
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 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

精彩评论

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