Skip to content

Instantly share code, notes, and snippets.

@cmcenearney
Created August 8, 2013 00:47
Show Gist options
  • Save cmcenearney/6180442 to your computer and use it in GitHub Desktop.
Save cmcenearney/6180442 to your computer and use it in GitHub Desktop.
threads 1
import java.io.*;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.perf4j.LoggingStopWatch;
import org.perf4j.StopWatch;
import java.io.File;
import java.io.IOException;
public class CopyFileRunnable implements Runnable {
private static final Logger logger = Logger.getLogger(CopyFileRunnable.class);
private static final int BUFFER_SIZE = 1024;
File source;
File destination;
public CopyFileRunnable(File source, File destination){
this.source = source;
this.destination = destination;
}
@Override
public void run() {
if (!source.isFile())
throw new IllegalArgumentException("Input file must be an actual file!");
try {
if (!destination.exists())
destination.createNewFile();
}
catch (IOException exception){
logger.error("Failed to creat new file: " + exception) ;
}
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(source);
out = new FileOutputStream(destination);
byte[] buf = new byte[BUFFER_SIZE];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
}
catch (IOException exception) {
logger.error("Failed to copy directory. Exception information: " + exception);
}
finally {
try {
if (in != null)
in.close();
if (out != null)
out.close();
}
catch (IOException e){
logger.error("exceptiopn!" + e);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment