상세 컨텐츠

본문 제목

FileInputStream, OutPutStream,File 객체를 이용한 파일 복사 및 성능 테스트

Java

by kwanghyup 2020. 6. 15. 03:25

본문

import java.io.*;

// 파일 복사와 성능 테스트
public class FileInputStreamExam02 {
    // 파일 복사
    public static void main(String[] args) throws IOException {
        String  originalFileName = FileInputStreamExam02.class
                .getResource("directory/originalfile.png").getPath(); //원본파일 경로
        String  copyFileName = FileInputStreamExam02.class
                .getResource("copy").getPath()+"/copyfile.png"; //

        File originlFile = new File(originalFileName); // 원본파일 객체 생성
        File copyFile = new File(copyFileName); // 사본 파일 객체 생성
        InputStream is = new FileInputStream(originlFile); // 원본 파일을 읽어서 read
        OutputStream os = new FileOutputStream(copyFile);   // 사본 파일에 쓰기  write


        long startTime = System.currentTimeMillis(); // 성능테스트 시작 시간
        int readByteNo; // 읽은 바이트 수
        int looping = 0; // 반복수 체크
        byte[] bytes = new byte[50000]; // 바이트 배열
        while (( readByteNo = is.read(bytes))!=-1) {
            os.write(bytes,0, readByteNo);
            os.flush();
            looping++;
        }
        long endTime = System.currentTimeMillis(); // 성능테스트 끝난 시간
        System.out.println("반복수: " + looping);
        System.out.println("소요시간: " + (endTime-startTime) +"milliseconds");
        System.out.println("원본 파일의 크기 : " + originalFileName.length());
        System.out.println("복사된 파일의 크기 : " + copyFileName.length());

        is.close();
        os.close();
    }

}

관련글 더보기

댓글 영역