IT/Programming / / 2023. 4. 20. 13:05

[JAVA] 동영상 복사 + 시간경과 사용 예제

반응형
package dayday18;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

/*
 * wildlife.wmv파일을 myVideo.wmv로 복사
 * 수행시간 몇초가 경과했는지 Thread로 구현하여 출력
 * 
 */

class Timers extends Thread {
	int time = 0;

	@Override
	public void run() {
		System.out.println("복사가 진행중입니다.");
		try {
			while (true) {
				System.out.println(time+"초 경과...");
				Thread.sleep(1000);
				time++;
			}
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

public class Quiz01 {
	public static void main(String[] args) throws IOException {

		Timers t = new Timers();
		t.setDaemon(true);
		t.start();

		File in = new File("Wildlife.wmv");
		File out = new File("myVideo.wmv");

		FileChannel inChannel = new FileInputStream(in).getChannel();
		FileChannel outChannel = new FileOutputStream(out).getChannel();
		/*
		 * 매우빠른속도로 읽고 쓸수 있다.
		 * 커널버퍼를 직접 핸들링하기 때문에 빠르다.
		 * 커널 - 하드웨어와 소프트웨어를 연결해주는 중간 연결다리(미들웨어) 어플리케이션 - 커널(우선순위)(드라이버) - IOInterface
		 * 기존 블로킹이 발생해서 느리다.
		 * 
		 */
		
		try {
			inChannel.transferTo(0, inChannel.size(), outChannel);
		} catch (IOException e) {
			throw e;
		} finally {
            if (inChannel != null) inChannel.close();
            if (outChannel != null) outChannel.close();
        }
		System.out.println("//////////////////////////");
		System.out.println("//복사가 완료되었습니다.\t//\n//" + t.time + "초 경과하였습니다.\t//");
		System.out.println("//////////////////////////");
		// 3초 경과하였습니다.
/*
		try {
			FileInputStream in = new FileInputStream("Wildlife.wmv");
			FileOutputStream out = new FileOutputStream("myVideo.wmv");

			while (true) {
				int tmp = in.read();
				if (tmp == -1) {
					break;
				}

				out.write(tmp);
			}
			System.out.println("복사가 완료되었습니다.\n" + t.time + "초 경과하였습니다.");
			in.close();
			out.close();

		} catch (FileNotFoundException e) { // TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//44초경과
*/

	}
}
반응형
  • 네이버 블로그 공유
  • 네이버 밴드 공유
  • 페이스북 공유
  • 카카오스토리 공유