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 |