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

I want to create a Java program that creates certain HTML files, and since they all contain some images, the program must also copy these images to user.dir, where the HTML files are created. I have these images in the "resources" of the package, the code in the package "code". How to do it?

+3
source share
1 answer

Basically, you will need a list of resource files that you want to copy. You use them

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();
    }
  }
}
+2
source

Source: https://habr.com/ru/post/1729105/


All Articles