- 2019/07/14 11:24
- zero2080.egloos.com/5355812
- 덧글수 : 0
- 2019/07/14 10:54
- zero2080.egloos.com/5355811
- 덧글수 : 0
- 2019/06/20 13:57
- zero2080.egloos.com/5354993
- 덧글수 : 0
- 2019/06/12 10:25
- zero2080.egloos.com/5354690
- 덧글수 : 0
- 2019/05/27 14:55
- zero2080.egloos.com/5354114
- 덧글수 : 0
Language: JAVA
Importpackage : java.nio
1. 요구사항 확인
- 파일 입/출력(복사)
- 커맨드입력을 통한 파일 복사
n 복사할 대상선택 : 경로를 포함한 파일명 입력
n 붙여넣을장소 선택 : 디렉토리 선택
- 예외 발생 시 예외 메시지 출력
2. 제약사항
- 약 1.5G 이상 복사 불가
3. 구조
- Commend클래스
n 해당 프로그램의기본 명령어 실행
u Ex> c:/Develop/test/test.jpg
u Ex> c:/Develop/test
- FileStream 클래스
n 선택한 파일과붙여넣을 경로의 디렉토리를 파일스트림에 연결한다.
- FileCopy 클래스
n NIO패키지의 채널을 이용하여 파일 복사 및 붙여넣기를 실행한다.
4. Code
A. Commend 클래스
public class Commend {
public Stringinput(String cmd) {
Scannerscanner = new Scanner(System.in);
if(cmd.equals("sourceFile")){
System.out.print("복사할 대상을 입력하세요 : ");
}elseif(cmd.equals("targetDirectory")) {
System.out.print("붙여넣을 경로를 입력하세요 : ");
}else {
System.out.println("잘못된 명령어 입니다.");
}
return scanner.next();
}
B. FileStream 클래스
public class FileStream {
privateFileInputStream fis = null;
privateFileOutputStream fos = null;
private StringfileName = null;
private Filefile = null;
publicFileInputStream setFis(String sourceFile) throws Exception{
file =new File(sourceFile);
fileName=sourceFile.substring(sourceFile.lastIndexOf("/")+1,sourceFile.length());
try {
if(file.isFile()){
fis= new FileInputStream(file);
}else{
thrownew Exception("FileInputStream exception : sourceFile(File notexist)");
}
}catch(Exceptione) {
System.out.println(e.getMessage());
}
returnfis;
}
publicFileOutputStream setFos(String targetDirectory) throws Exception{
try {
Filetemp = new File(targetDirectory);
fileName= System.currentTimeMillis()+fileName;
if(temp.isDirectory()){
fos= new FileOutputStream(targetDirectory+"/"+fileName);
}else{
thrownew Exception("FileOutputStream exception : targetDirectory(Directory notexist)");
}
}catch(Exceptione) {
if(e.getMessage().equals("null")){
System.out.println("FileStream()Exception : NullPointException");
}
}
returnfos;
}
public voidclose() {
try {
if(fos!=null){
fos.close();
}
}catch(Exceptione) {
System.out.println("FileStreamclose() Exception : "+e.getMessage());
}
try {
if(fis!=null){
fis.close();
}
}catch(Exceptione) {
System.out.println("FileStreamclose() Exception : "+e.getMessage());
}
}
public StringgetFileName() {
returnfileName;
}
publicFileInputStream getFis() {
returnfis;
}
}
C. FileCopy 클래스
public class FileCopy {
public Stringcopy(String sourceFile, String targetDirectory) {
FileStreamfs = new FileStream();
FileChannelfileInput = null;
FileChannelfileOutput = null;
StringfileName =sourceFile.substring(sourceFile.lastIndexOf("/")+1,sourceFile.length());
System.out.println(sourceFile+ " / " + targetDirectory + " / " +fileName);
longstart = System.currentTimeMillis();
try {
fileInput= fs.setFis(sourceFile).getChannel();
fileOutput= fs.setFos(targetDirectory).getChannel();
ByteBufferbuff = ByteBuffer.allocateDirect(fs.getFis().available());
fileInput.read(buff);
buff.flip();
fileOutput.write(buff);
}catch(Exception e) {
System.out.println("FileStreamExcepiont() : "+e.getMessage());
}finally{
fs.close();
close(fileInput);
close(fileOutput);
}
System.out.println((System.currentTimeMillis()-start)/1000);
return"파일복사가 완료 되었습니다.";
}
private voidclose(Closeable obj) {
try {
if(obj!= null) {
obj.close();
}
}catch(Exceptione) {
}
}
}
5. 개선사항
A. 요구사항을 정확히 확인 후 설계한다.
B. 설계 후 코드 작성에 돌입한다.
C. 객체화(모듈화)하여작성한다.
D. 대용량 파일을 처리하기위해선 스트림에 연결된 파일을 분할하여 처리 할필요가 있으며, ByteBuffer의 put과 get을 이용해 가능할 것으로 보인다.



최근 덧글