|
|
@ -0,0 +1,68 @@ |
|
|
|
package com.jesuspinar.main; |
|
|
|
|
|
|
|
import java.io.*; |
|
|
|
|
|
|
|
public class Main{ |
|
|
|
public static void main(String[] arg) throws Exception { |
|
|
|
copyFile("/Users/jesuspl/Documents/04DamPrac1/PRG/tema12/ficheros/nuevo.txt"); |
|
|
|
} |
|
|
|
private static boolean copyFile(String path){ |
|
|
|
File inputFile = new File(path); |
|
|
|
|
|
|
|
try (FileInputStream fis = new FileInputStream(inputFile); |
|
|
|
FileOutputStream fos = |
|
|
|
new FileOutputStream(path+"cp"+ getExtension(inputFile)) |
|
|
|
// fos.write(fis.readAllBytes()); |
|
|
|
){ |
|
|
|
StringBuilder sb = new StringBuilder(); |
|
|
|
long fileSize = inputFile.length(); |
|
|
|
double onePercent = (double) 100/fileSize; |
|
|
|
long count = 0; |
|
|
|
int character ; |
|
|
|
int i = 0; |
|
|
|
while((character = fis.read()) != -1){ |
|
|
|
count += character; |
|
|
|
if (count >= onePercent) { |
|
|
|
count = 0; |
|
|
|
sb.append("#"); |
|
|
|
System.out.print(++i +"% "+ sb ); |
|
|
|
System.out.print("\r"); |
|
|
|
Thread.sleep(100); |
|
|
|
} |
|
|
|
fos.write(character); |
|
|
|
} |
|
|
|
return true; |
|
|
|
} catch (FileNotFoundException e) { |
|
|
|
throw new RuntimeException(e); |
|
|
|
} catch (IOException e) { |
|
|
|
throw new RuntimeException(e); |
|
|
|
} catch (InterruptedException e) { |
|
|
|
throw new RuntimeException(e); |
|
|
|
} |
|
|
|
} |
|
|
|
private static void progressPrint(){ |
|
|
|
StringBuilder sb = new StringBuilder(); |
|
|
|
|
|
|
|
for (int i = 0 ; i <= 100 ; i++) { |
|
|
|
sb.setLength(0); |
|
|
|
for (int j = 0 ; j < i; j++) { |
|
|
|
sb.append("#"); |
|
|
|
} |
|
|
|
try { |
|
|
|
Thread.sleep(100); |
|
|
|
} catch (InterruptedException e) { |
|
|
|
throw new RuntimeException(e); |
|
|
|
} |
|
|
|
System.out.print("[" + String.format("%-100s", sb) + "] " + i + "%"); |
|
|
|
System.out.print("\r"); |
|
|
|
} |
|
|
|
} |
|
|
|
private static String getExtension(File file){ |
|
|
|
String fileName = file.toString(); |
|
|
|
int index = fileName.lastIndexOf('.'); |
|
|
|
if(index > 0) { |
|
|
|
return "." + fileName.substring(index + 1); |
|
|
|
} |
|
|
|
return ".dat"; |
|
|
|
} |
|
|
|
} |