package com.janosgyerik.utils.files;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;

public class SimpleArchiver {

    private final File targetDir;

    public SimpleArchiver(File targetDir) {
        this.targetDir = targetDir;
    }

    public File getTargetWithDate(File source) {
        String datepart = new SimpleDateFormat("yyyyMMdd").format(new Date());
        return new File(targetDir, datepart + "_" + source.getName());
    }

    private void prepareTargetDir() throws IOException {
        if (!targetDir.isDirectory() && !targetDir.mkdirs()) {
            throw new IOException("Could not create target directory: " + targetDir);
        }
    }

    public void copyWithDate(File source) throws IOException {
        prepareTargetDir();
        File target = getTargetWithDate(source);
        copy(source, target);
    }

    public void moveWithDate(File source) throws IOException {
        prepareTargetDir();
        File target = getTargetWithDate(source);
        copy(source, target);
        if (!source.delete()) {
            throw new IOException("Could not delete source: " + source);
        }
    }

    private void copy(File source, File target) throws IOException {
        InputStream input = null;
        OutputStream output = null;
        try {
            input = new FileInputStream(source);
            output = new FileOutputStream(target);
            byte[] buf = new byte[1024];
            int bytesRead;
            while ((bytesRead = input.read(buf)) > 0) {
                output.write(buf, 0, bytesRead);
            }
        } finally {
            if (input != null) {
                input.close();
            }
            if (output != null) {
                output.close();
            }
        }
    }
}

@@ -1240,32 +1240,35 @@
     
-InputStream input = null
+byte%5B%5D buf = new byte%5B1024%5D
 ;%0A  
@@ -1273,19 +1273,23 @@
         
-Out
+try (In
 putStrea
@@ -1294,49 +1294,8 @@
 eam 
-output = null;%0A        try %7B%0A            
 inpu
@@ -1335,24 +1335,38 @@
             
+ OutputStream 
 output = new
@@ -1395,49 +1395,11 @@
 et);
-%0A            byte%5B%5D buf = new byte%5B1024%5D;
+) %7B
 %0A   
@@ -1545,186 +1545,8 @@
   %7D%0A
-        %7D finally %7B%0A            if (input != null) %7B%0A                input.close();%0A            %7D%0A            if (output != null) %7B%0A                output.close();%0A            %7D%0A
     

Rewrite using try-with-resources