JAVA

java directory copy / progress bar

2022. 6. 11. 22:13
728x90
๋ฐ˜์‘ํ˜•

 

๐Ÿ˜’ test์šฉ์œผ๋กœ ๊ตฌํ˜„ java 1.8 ์‚ฌ์šฉ 

 

๐Ÿ˜’ ์—์ด์ „ํŠธ์˜ FS ๊ธฐ๋Šฅ ๊ฐœ๋ฐœ ์ค‘ ํ•„์š”ํ•ด์„œ ๊ตฌํ˜„.

 

 

ํŠน์ • ๋””๋ ‰ํ† ๋ฆฌ ๊ฒฝ๋กœ ๋ฐ ๋ชจ๋“  ํ•˜์œ„ ํŒŒ์ผ์„ file output stream์œผ๋กœ ์“ฐ๋ฉด์„œ ์†Œ์Šค์™€ ํƒ€๊ฒŸ์˜ ๋ฐ”์ดํŠธ๋‹จ์œ„ ํฌ๊ธฐ๋ฅผ ๋น„๊ตํ•œ๋‹ค.

์žฌ๊ท€ํ˜ธ์ถœ์„ ํ†ตํ•ด ๋””๋ ‰ํ† ๋ฆฌ์•ˆ์˜ ๋””๋ ‰ํ† ๋ฆฌ๋„ ๋ชจ๋‘ ํ™•์ธํ•˜๋„๋ก ๊ตฌํ˜„.

 

1. static ๋ณ€์ˆ˜๋ฅผ ํ†ตํ•ด ์ง„ํ–‰๋ฅ  ๋“ฑ ๊ธฐ๋กํ•˜๋Š” ๋ฐฉ์‹. 

2. static ๋ณ€์ˆ˜ ์—†์ด, ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ์‹.

     ๋ฉ€ํ‹ฐ  ์Šค๋ ˆ๋“œ ํ™˜๊ฒฝ์—์„œ ์ „์—ญ๋ณ€์ˆ˜๋ฅผ ์ฐธ์กฐํ•˜๋ฉด ์ „์—ญ๋ณ€์ˆ˜์˜ ๊ฐ’์ด ๊ผฌ์ด๋ฏ€๋กœ, 2๋ฒˆ์˜ ๋ฐฉ์‹์ด ์ ํ•ฉํ•จ.

 

 

FileDownloaderTest.java

package test;

import com.gemiso.ariel.common.util.FileUtil;

import java.io.*;

public class FsCopyDirTest {

    static long totalBytesCopied;
    static long expectedBytes;

    public static void main(String[] args) {
        String srcDirPath = "C:\\Users\\user\\Desktop\\ํ…Œ์ŠคํŠธ์šฉ ์˜์ƒ ๋ฐ ์ด๋ฏธ์ง€\\๋””๋ ‰ํ† ๋ฆฌ ์นดํ”ผ ํ…Œ์ŠคํŠธ์šฉ 1";
        //String srcDirPath = "C:\\Users\\admin\\Desktop\\๋‹ค์šด๋กœ๋“œ ์†Œ์Šค";
        String tgtDirPath = "C:\\Users\\user\\Desktop\\ํ…Œ์ŠคํŠธ์šฉ ์˜์ƒ ๋ฐ ์ด๋ฏธ์ง€\\๋””๋ ‰ํ† ๋ฆฌ ์นดํ”ผ ํƒ€๊ฒŸ";
        //String tgtDirPath = "C:\\Users\\admin\\Desktop\\๋‹ค์šด๋กœ๋“œ ํƒ€๊ฒŸ";

        File srcDir = new File(srcDirPath);
        File tgtDir = new File(tgtDirPath);

        expectedBytes = FileUtil.getDirSize(srcDir);
        
        // ์ „์—ญ๋ณ€์ˆ˜ ์—†์ด ์‹คํ–‰ํ•˜๊ฒŒ๋œ๋‹ค๋ฉด 0๊ฐ’์œผ๋กœ ์‹คํ–‰.
        // expectedBytes = Fileutil.getDirSize(srcDir,0);
        
        copy(srcDir, tgtDir);
    }


    public static void copy(File srcDir, File tgtDir) {

        boolean isDirectory = srcDir.isDirectory();

        String[] srcList = srcDir.list();

        boolean is_cancel = false;

        try {
            int len = 0;
            byte[] buf = new byte[1024];
            int rates = 0;

            System.out.println(" ์นดํ”ผ ์‹œ์ž‘! ");

            File finalTgtDir = tgtDir;
            File[] target_file = srcDir.listFiles();

            // ํด๋” ์‚ฌ์ด์ฆˆ๋ฅผ ๊ตฌํ•  ๊ฒฝ์šฐ ํ•˜์œ„ํŒŒ์ผ์„ ๋ชจ๋‘ ํƒ์ƒ‰ํ•˜๊ณ  ํŒŒ์ผ ํฌ๊ธฐ ํ•ฉ์„ ๊ตฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด
            // directory์˜ ์‚ฌ์ด์ฆˆ๋ฅผ 4096์œผ๋กœ ๊ณ ์ •ํ•ด์„œ ๋ฑ‰์–ด๋‚ธ๋‹ค.

            System.out.println(" ์†Œ์Šค ํŒŒ์ผ ์‚ฌ์ด์ฆˆ : " + expectedBytes);

            for (File file : target_file) {
                File temp = new File(tgtDir.getAbsolutePath() + File.separator + file.getName());
                if (file.isDirectory()) {
                    temp.mkdir();
                    copy(file, temp);
                } else {

                    FileInputStream fis = null;
                    FileOutputStream fos = null;
                    try {
                        fis = new FileInputStream(file);
                        fos = new FileOutputStream(temp);
                        byte[] b = new byte[1024];
                        while ((len = fis.read(b)) != -1) {
                            totalBytesCopied += len;

                            fos.write(b, 0, len);

                            rates = (int) Math.floor(( totalBytesCopied * 1.0 / expectedBytes) * 1000) / 10;

                            System.out.println(" ํŒŒ์ผ ๋ณ„ ์‚ฌ์ด์ฆˆ : " + totalBytesCopied + " / ์›๋ณธ dir ์‚ฌ์ด์ฆˆ : " + expectedBytes);
                            System.out.println(" ์ง„ํ–‰๋ฅ  : " + rates);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            fis.close();
                            fos.close();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }

                }
            }

        } catch (Exception e) {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            PrintStream pinrtStream = new PrintStream(out);
            e.printStackTrace(pinrtStream);
            System.out.println(out.toString());
        }

    }
    
    
    // ๋ฉ€ํ‹ฐ์Šค๋ ˆ๋“œ ํ™˜๊ฒฝ์—์„œ ์Šคํƒœํ‹ฑ ๋ณ€์ˆ˜๋ฅผ ์ฐธ์กฐํ•˜๊ณ  ์‹ถ์ง€ ์•Š์€ ๊ฒฝ์šฐ ์•„๋ž˜ ๋กœ์ง ์ˆ˜ํ–‰ : 
     public static void copy(File srcDir, File tgtDir, long totalBytesCopied, long expectedBytes) {

        boolean isDirectory = srcDir.isDirectory();

        String[] srcList = srcDir.list();

        boolean is_cancel = false;

        try {
            int len = 0;
            byte[] buf = new byte[1024];

            System.out.println(" ์นดํ”ผ ์‹œ์ž‘! ");

            File finalTgtDir = tgtDir;
            File[] target_file = srcDir.listFiles();

            for (File file : target_file) {
                File temp = new File(tgtDir.getAbsolutePath() + File.separator + file.getName());
                if (file.isDirectory()) {
                    temp.mkdir();
                    copy(file, temp, totalBytesCopied, expectedBytes);
                } else {

                    FileInputStream fis = null;
                    FileOutputStream fos = null;
                    DataOutputStream dos = null;
                    try {
                        fis = new FileInputStream(file);
                        fos = new FileOutputStream(temp);
                        dos = new DataOutputStream(fos);


                        byte[] b = new byte[1024];
                        while ((len = fis.read(b)) != -1) {
                            totalBytesCopied += len;
                            dos.write(b, 0, len);

                            FileUtil.rates = (int) Math.floor(( totalBytesCopied * 1.0 / expectedBytes) * 1000) / 10;

                            System.out.println(" ํŒŒ์ผ ๋ณ„ ์‚ฌ์ด์ฆˆ : " + totalBytesCopied + " / ์›๋ณธ dir ์‚ฌ์ด์ฆˆ : " + expectedBytes + " / rate : " + FileUtil.rates);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            fis.close();
                            fos.close();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }

                }
            }

        } catch (Exception e) {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            PrintStream pinrtStream = new PrintStream(out);
            e.printStackTrace(pinrtStream);
            System.out.println(out.toString());
        }

    }

    
    
    
    
    
}

 

 

FileUtil.java

package com.org.walk.util;

import java.io.File;
import java.io.IOException;

public class Utils {

 	public static long getDirSize(File folder) {
		File[] files = folder.listFiles();

		long length=0;

		int count = files.length;
		for (int i = 0; i < count; i++) {
			if (files[i].isFile()) {
				// System.out.println(" ํŒŒ์ผ ๋„ค์ž„ : " + files[i].getName() + " / size : " + files[i].length());
				length += files[i].length();
			} else {
				// System.out.println(" DIR ๋„ค์ž„ : " + files[i].getName());
				length += getDirSize(files[i]);
			}
		}
		return length;
	}
    
    
    // ๋ฉ€ํ‹ฐ ์Šค๋ ˆ๋“œ ํ™˜๊ฒฝ์—์„œ static ๋ณ€์ˆ˜๋ฅผ ๋™์‹œ์— ์ฐธ์กฐํ•˜๊ณ  ์‹ถ์ง€ ์•Š์€ ๊ฒฝ์šฐ๋ผ๋ฉด ์•„๋ž˜ ๋กœ์ง ์ˆ˜ํ–‰ : 
    public static long getDirSize(File folder, long length) {
		File[] files = folder.listFiles();

		long _length;

		if (length!=0) {
			_length = length;
		} else {
			_length = 0;
		}

		int count = files.length;
		for (int i = 0; i < count; i++) {
			if (files[i].isFile()) {
				
				_length += files[i].length();
				
			} else {
				
				_length += getDirSize(files[i],0);
				
			}
		}

		return _length;
	}
    
    
    
    
}

 

 

 

 

 

 

 

 

 

320x100
๋ฐ˜์‘ํ˜•
์ €์ž‘์žํ‘œ์‹œ (์ƒˆ์ฐฝ์—ด๋ฆผ)

'JAVA' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Java] TDD์™€ ์˜ˆ์ œ์™€ ์—ฐ์Šต , querydsl ์Šฌ๋ผ์ด์‹ฑ ํ…Œ์ŠคํŠธ ์„ค์ •  (0) 2023.02.25
[JAVA] Gabage Collection  (0) 2022.12.13
springboot / querydsl ์„ค์ •  (0) 2022.05.31
jwt ํ† ํฐ ์ธ์ฆ + Interceptor ๊ฒ€์ฆ.  (0) 2022.05.22
java ์ •๊ทœ ํ‘œํ˜„์‹  (0) 2022.05.09
'JAVA' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
  • [Java] TDD์™€ ์˜ˆ์ œ์™€ ์—ฐ์Šต , querydsl ์Šฌ๋ผ์ด์‹ฑ ํ…Œ์ŠคํŠธ ์„ค์ •
  • [JAVA] Gabage Collection
  • springboot / querydsl ์„ค์ •
  • jwt ํ† ํฐ ์ธ์ฆ + Interceptor ๊ฒ€์ฆ.
girin_dev
girin_dev
๊ธฐ๋กํ•ฉ์‹œ๋‹ค.
250x250
girin_dev
girin_dev
girin_dev

github.com/jaemanc


์ „์ฒด
์˜ค๋Š˜
์–ด์ œ
  • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (122)
    • ALGORITHM (23)
    • AWS (4)
    • Effective Java (4)
    • ERROR (12)
    • DB (11)
    • JAVA (23)
      • SPRING (10)
    • PYTHON (5)
      • TOY_PROJECT (1)
    • MOBILE (4)
    • SERVER (8)
    • TIPS (16)
    • WAS (2)
    • ์ƒˆ์‹น ์ผ๊ธฐ (5)
    • DATA (2)

๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

  • ํ™ˆ
  • ํƒœ๊ทธ
  • ๋ฐฉ๋ช…๋ก

๊ณต์ง€์‚ฌํ•ญ

์ธ๊ธฐ ๊ธ€

ํƒœ๊ทธ

  • JAVA 11
  • Effective Java
  • oracle cloud
  • springboot
  • centos7
  • IntelliJ
  • ๋ฐ”์งˆ ํ‚ค์šฐ๊ธฐ
  • docker
  • ๋ฐ”์งˆ
  • Chat GPT
  • offset
  • python3
  • error
  • ๋ฐ”๋‘‘์ด
  • CentOS 8
  • dp
  • oracle
  • ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค
  • vertica
  • ๋‹ค์ด๋‚˜๋ฏน ํ”„๋กœ๊ทธ๋ž˜๋ฐ
  • spring boot
  • ๊ฐ€์žฅ ํฐ ์ˆ˜
  • ์ƒˆ์‹น
  • java
  • react-native
  • Flutter
  • querydsl
  • ๋ฐ”์งˆ ํŽ˜์Šคํ† 
  • lis
  • jwt

์ตœ๊ทผ ๋Œ“๊ธ€

์ตœ๊ทผ ๊ธ€

hELLO ยท Designed By ์ •์ƒ์šฐ.
girin_dev
java directory copy / progress bar
์ƒ๋‹จ์œผ๋กœ

ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”

๋‹จ์ถ•ํ‚ค

๋‚ด ๋ธ”๋กœ๊ทธ

๋‚ด ๋ธ”๋กœ๊ทธ - ๊ด€๋ฆฌ์ž ํ™ˆ ์ „ํ™˜
Q
Q
์ƒˆ ๊ธ€ ์“ฐ๊ธฐ
W
W

๋ธ”๋กœ๊ทธ ๊ฒŒ์‹œ๊ธ€

๊ธ€ ์ˆ˜์ • (๊ถŒํ•œ ์žˆ๋Š” ๊ฒฝ์šฐ)
E
E
๋Œ“๊ธ€ ์˜์—ญ์œผ๋กœ ์ด๋™
C
C

๋ชจ๋“  ์˜์—ญ

์ด ํŽ˜์ด์ง€์˜ URL ๋ณต์‚ฌ
S
S
๋งจ ์œ„๋กœ ์ด๋™
T
T
ํ‹ฐ์Šคํ† ๋ฆฌ ํ™ˆ ์ด๋™
H
H
๋‹จ์ถ•ํ‚ค ์•ˆ๋‚ด
Shift + /
โ‡ง + /

* ๋‹จ์ถ•ํ‚ค๋Š” ํ•œ๊ธ€/์˜๋ฌธ ๋Œ€์†Œ๋ฌธ์ž๋กœ ์ด์šฉ ๊ฐ€๋Šฅํ•˜๋ฉฐ, ํ‹ฐ์Šคํ† ๋ฆฌ ๊ธฐ๋ณธ ๋„๋ฉ”์ธ์—์„œ๋งŒ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.