|
@ -0,0 +1,79 @@ |
|
|
|
|
|
package Ejercicio6; |
|
|
|
|
|
|
|
|
|
|
|
import java.io.File; |
|
|
|
|
|
import java.io.FileInputStream; |
|
|
|
|
|
import java.io.FileNotFoundException; |
|
|
|
|
|
import java.io.FileOutputStream; |
|
|
|
|
|
import java.io.IOException; |
|
|
|
|
|
import java.nio.file.FileSystems; |
|
|
|
|
|
import java.nio.file.Files; |
|
|
|
|
|
import java.nio.file.Path; |
|
|
|
|
|
import java.nio.file.Paths; |
|
|
|
|
|
import java.nio.file.StandardCopyOption; |
|
|
|
|
|
import java.util.Scanner; |
|
|
|
|
|
|
|
|
|
|
|
public class Ejercicio6 { |
|
|
|
|
|
|
|
|
|
|
|
// Crea un programa Java MiCopy que copie un archivo. Por ejemplo “MiCopy |
|
|
|
|
|
// FicheroOrigen.dat FicheroDestino.dat”. |
|
|
|
|
|
// El programa tendrá que mostrar por pantalla el tanto por cien que lleva |
|
|
|
|
|
// realizada de la copia y deberá también mostrar algún tipo de animación |
|
|
|
|
|
// (similar a wget). |
|
|
|
|
|
// Cuando termine la copia se mostrará un mensaje de fin de la copia. |
|
|
|
|
|
private static String formatter ="\033[H\033[2J | Descarga %2.2f%% %1s %s | \n"; |
|
|
|
|
|
private static Scanner sc = new Scanner(System.in); |
|
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
System.out.println("Introduce la ruta de origen"); |
|
|
|
|
|
String origen = sc.nextLine(); |
|
|
|
|
|
System.out.println("Introduce la ruta de destino"); |
|
|
|
|
|
String destino = sc.nextLine(); |
|
|
|
|
|
|
|
|
|
|
|
copiar(origen, destino); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void copiar(String origen, String destino) { |
|
|
|
|
|
|
|
|
|
|
|
File sourceFile = new File(origen); |
|
|
|
|
|
File destinationFile = new File(destino); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try ( |
|
|
|
|
|
FileInputStream inputStream = new FileInputStream(sourceFile); |
|
|
|
|
|
FileOutputStream outputStream = new FileOutputStream(destinationFile)) { |
|
|
|
|
|
|
|
|
|
|
|
long lenOfFile = sourceFile.length(); |
|
|
|
|
|
long currentBytesWritten = 0; |
|
|
|
|
|
int data; |
|
|
|
|
|
StringBuilder sb = new StringBuilder(); |
|
|
|
|
|
|
|
|
|
|
|
while ((data = inputStream.read()) != -1) { |
|
|
|
|
|
outputStream.write(data); |
|
|
|
|
|
String symbol = "*"; |
|
|
|
|
|
currentBytesWritten += 1; |
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i <= 1; i++) { |
|
|
|
|
|
|
|
|
|
|
|
for (int j = 0; j <= i; j++) { |
|
|
|
|
|
sb.append(symbol); |
|
|
|
|
|
System.out.printf(formatter, 100 * ((double) currentBytesWritten) / ((double) lenOfFile), " ", sb.append(sb.charAt(j))); |
|
|
|
|
|
} |
|
|
|
|
|
Thread.sleep(40); |
|
|
|
|
|
System.out.flush(); |
|
|
|
|
|
} |
|
|
|
|
|
System.out.println("Copia completada exitosamente!"); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} catch (InterruptedException | IOException io) { |
|
|
|
|
|
io.printStackTrace(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |