Useful if org.apache.commons.io.FileUtils not available.
This commit is contained in:
parent
b36e4dfb8d
commit
f432dc1e2c
1 changed files with 59 additions and 0 deletions
59
src/fr/devinsy/util/FileCopier.java
Normal file
59
src/fr/devinsy/util/FileCopier.java
Normal file
|
@ -0,0 +1,59 @@
|
|||
package fr.devinsy.util;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
|
||||
|
||||
/**
|
||||
* Never used. Prefer org.apache.commons.io.FileUtils class.
|
||||
*/
|
||||
public class FileCopier
|
||||
{
|
||||
static final public int BUFFER_SIZE = 4*1024;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
static void copy (File source, File target) throws Exception
|
||||
{
|
||||
if ((source == null) || (target == null))
|
||||
{
|
||||
throw new Exception("Null parameter.");
|
||||
}
|
||||
else
|
||||
{
|
||||
FileInputStream in = new FileInputStream(source);
|
||||
FileOutputStream out = new FileOutputStream(target);
|
||||
try
|
||||
{
|
||||
byte[] buffer = new byte[BUFFER_SIZE];
|
||||
boolean ended = false;
|
||||
while (!ended)
|
||||
{
|
||||
int size = in.read(buffer);
|
||||
if (size == -1)
|
||||
{
|
||||
ended = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
out.write(buffer, 0, size);
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (in != null)
|
||||
{
|
||||
in.close();
|
||||
}
|
||||
|
||||
if (out != null)
|
||||
{
|
||||
out.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue