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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| public class dep {
public static boolean copyFile(File source, File dest){
try{
// Declaration et ouverture des flux
java.io.FileInputStream sourceFile = new java.io.FileInputStream(source);
try{
java.io.FileOutputStream destinationFile = null;
try{
destinationFile = new FileOutputStream(dest);
// Lecture par segment de 0.5Mo
byte buffer[] = new byte[512 * 1024];
int nbLecture;
while ((nbLecture = sourceFile.read(buffer)) != -1){
destinationFile.write(buffer, 0, nbLecture);
}
} finally {
destinationFile.close();
}
} finally {
sourceFile.close();
}
} catch (IOException e){
e.printStackTrace();
return false; // Erreur
}
return true; // Résultat OK
}
private static boolean copier(File source, File destination) {
// TODO Auto-generated method stub
FileChannel in = null; // canal d'entrée
FileChannel out = null; // canal de sortie
try {
// Init
in = new FileInputStream("C:/toto.txt").getChannel();
out = new FileOutputStream("C:/tutu.txt").getChannel();
// Copie depuis le in vers le out
in.transferTo(0, in.size(), out);
} catch (Exception e) {
e.printStackTrace(); // n'importe quelle exception
} finally { // finalement on ferme
if(in != null) {
try {
in.close();
} catch (IOException e) {}
}
if(out != null) {
try {
out.close();
} catch (IOException e) {}
}
}
return false;
}
} |
Partager