반응형
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초경과
*/
}
}
반응형
'IT > Programming' 카테고리의 다른 글
[JAVA] ObjectOutputStream,InputStream 사용 예제 (0) | 2023.04.20 |
---|---|
[JAVA] 파일 객체정보 읽어들이기 (0) | 2023.04.20 |
[JAVA] 객체를 파일로 저장 및 출력 사용 예제 (0) | 2023.04.20 |
[JAVA, HTML] Servlet(web.xml수정) (0) | 2023.04.20 |
[JAVA, HTML] - Servlet(Anotation으로 호출) (1) | 2023.04.19 |