Du speicherst dir den FileInputStream in keiner Variablen, damit hast du auch keine Möglichkeit den zu schließen.
public static void copyFile(File in, File out) throws IOException {
FileChannel inChannel = new FileInputStream(in).getChannel();
FileChannel outChannel = new FileOutputStream(out).getChannel();
inChannel.transferTo(0, inChannel.size(), outChannel);
inChannel.close();
outChannel.close();
}
public static void copyFile(File in, File out) throws IOException {
FileInputStream fileInputStream = new FileInputStream(in);
FileOutputStream fileOutputStream = new FileOutputStream(out);
FileChannel inChannel = fileInputStream.getChannel();
FileChannel outChannel = fileOutputStream.getChannel();
inChannel.transferTo(0, inChannel.size(), outChannel);
fileInputStream.close();
fileOutputStream.close();
}
public static void copyFile(File in, File out) throws IOException {
try(FileInputStream fileInputStream = new FileInputStream(in);
FileOutputStream fileOutputStream = new FileOutputStream(out);
FileChannel inChannel = fileInputStream.getChannel();
FileChannel outChannel = fileOutputStream.getChannel();) {
inChannel.transferTo(0, inChannel.size(), outChannel);
}
}
public static void copyFile(File in, File out) throws IOException {
Files.copy( in.toPath(), out.toPath() );
}
public static void copyFile(File in, File out) throws IOException {
FileInputStream fileInputStream = new FileInputStream(in);
FileOutputStream fileOutputStream = new FileOutputStream(out);
FileChannel inChannel = fileInputStream.getChannel();
FileChannel outChannel = fileOutputStream.getChannel();
inChannel.transferTo(0, inChannel.size(), outChannel);
fileInputStream.close();
fileOutputStream.close();
out.setLastModified(in.lastModified());
}