1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
public static synchronized void copieFichier(File source, File destination) throws IOException
{
RandomAccessFile rafSource = new RandomAccessFile(source, "rw");
FileChannel fcSource = rafSource.getChannel();
FileLock verrouSource = fcSource.lock();
RandomAccessFile rafDestination = new RandomAccessFile(destination, "rw");
FileChannel fcDestination = rafDestination.getChannel();
FileLock verrouDestination = fcDestination.lock();
FileInputStream sourceFile = null;
FileOutputStream destinationFile = null;
// creation du fichier
destination.createNewFile();
// ouverture des flux
sourceFile = new FileInputStream(source);
destinationFile = new FileOutputStream(destination);
// lecture par segment de 0.5 Mo
byte buffer[] = new byte[512 * 1024];
int nbLecture = -1;
while ((nbLecture = sourceFile.read(buffer)) != -1)
{
destinationFile.write(buffer, 0, nbLecture);
}
sourceFile.close();
destinationFile.close();
verrouSource.release();
fcSource.close();
rafSource.close();
verrouDestination.release();
fcDestination.close();
rafDestination.close();
} |