Euh j'ai peut-être pas saisi le problème... Mais moi je répondrais tout simplement:
File destination = new File("C:\\TEMP");
Si tu bosse en java >=1.4, il y a beaucoup mieux pour la copie de fichiers/dossiers... en utilisant java.nio
Cadeau 
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
public static void copy(File source, File target) throws IOException {
if (source.isDirectory()) {
if (!target.exists() && !target.mkdirs()) {
throw new IOException("Impossible de créer le répertoire " +
target.getAbsolutePath());
}
File[] files;
try {
files = source.listFiles();
} catch (InternalError e) {
//FIX Bug #5056583 sometimes thrown by listFiles and shortcuts!
files = new File[0];
}
for (File sourceFile : files) {
String path = sourceFile.getAbsolutePath();
File targetFile = new File(target.getAbsolutePath() +
path.substring(path.lastIndexOf(System.getProperty("file.separator"))));
copy(sourceFile, targetFile);
}
} else if (source.exists()) {
FileChannel in = null; // canal d'entrée
FileChannel out = null; // canal de sortie
try {
// Init
in = new FileInputStream(source).getChannel();
out = new FileOutputStream(target).getChannel();
// Copie depuis le in vers le out
in.transferTo(0, in.size(), out);
} finally { // finalement on ferme
if (in != null) {
try {
in.close();
} catch (IOException e) {
//
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
//
}
}
}
} else {
throw new IOException("File doesn't exist: " + source.getAbsolutePath());
}
Thread.yield();
} |
et en ajoutant les imports...
1 2 3 4 5 6 7
|
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel; |
Partager