Browse Source

Fixing progress bar overload

main
Jesus 2 years ago
parent
commit
0e552984f1
1 changed files with 24 additions and 15 deletions
  1. +24
    -15
      src/com/jesuspinar/main/Main.java

+ 24
- 15
src/com/jesuspinar/main/Main.java View File

@ -4,7 +4,12 @@ import java.io.*;
public class Main{
public static void main(String[] arg) throws Exception {
copyFile("/Users/jesuspl/Documents/04DamPrac1/PRG/tema12/ficheros/nuevo.txt");
boolean isCopied = copyFile("/Users/jesuspl/Documents/04DamPrac1/PRG/tema12/ficheros/archivo01.txt");
if (isCopied){
System.out.println("Done !");
}else {
System.out.println("File could be copied !");
}
}
private static boolean copyFile(String path){
File inputFile = new File(path);
@ -12,24 +17,28 @@ public class Main{
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 ;
byte[] content = fis.readAllBytes();
double pctLoad = 0.01; // 1 = 100%
double loadFraction = (pctLoad * content.length);
int loadIndex = 1;
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);
for (int j = 0 ; j <= content.length-1; j++){
if (loadIndex >= (int) loadFraction){
loadIndex = 0;
if (i < 100){
sb.append("#");
System.out.print((++i) +"% "+ sb );
System.out.print("\r");
Thread.sleep(50);
}
}
fos.write(character);
//Action of copy
loadIndex++;
fos.write(content[j]);
}
return true;
} catch (FileNotFoundException e) {


Loading…
Cancel
Save