package edu.exam14.ex01bytefilecopy;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

//입출력스트림을 열고 
//읽어서 저장하고
//닫는다.
public class ByteFileCopy {
	public static void main(String[] args) throws IOException {
		// 1단계 생성자 내부에서 open 과정이 일어난다
		InputStream in = new FileInputStream("putty.exe");
		OutputStream out = new FileOutputStream("푸푸푸푸푸푸푸푸푸티.exe");
		int copyByte = 0;
		// byte는 1바이트 자료형이지만
		// 표현범위가 0 ~ 255이므로 -1을 표현할 수 없다. 
		// 그래서 int받고 대신 int내부에 하위 1바이트만 읽어들이고
		// 1바이트만 저장한다.
		int bData;
		long sTime = System.currentTimeMillis();
		while(true) {
			// 1byte씩 읽어들인다.
			bData = in.read();	// 2단계 read
			if(bData == -1)	// 더이상 읽을 것이 없을 때
				break;
			out.write(bData);	// 2단계 write
			copyByte++;			// 몇바이트 읽었다(저장했다)
			//System.out.println(copyByte);
		}
		long eTime = System.currentTimeMillis();
		
		// 3단계
		in.close();
		out.close();
		
		System.out.println("복사된 바이트 크기: " + copyByte);
		System.out.println("복사에 소요된 시간: " + (eTime-sTime));
	}
}
package edu.exam14.ex02bufferfilecopy;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

//입출력스트림을 열고 
//읽어서 저장하고
//닫는다.
public class BufferFileCopy {
	public static void main(String[] args) throws IOException {
		InputStream in = new FileInputStream("putty.exe");
		OutputStream out = new FileOutputStream("푸티1.exe");
	
		int copyByte = 0;
		int readLen=0;
		
		byte[] buf = new byte[1024];
		
		long sTime = System.currentTimeMillis();
		while(true) {
			readLen = in.read(buf);	//2단계 read
			if(readLen==-1)	//더 이상 읽을 것이 없다
				break;
			
			//buf 배열에서 index0부터 readLen크기까지 저장해라
			out.write(buf,0,readLen); //2단계 write
			copyByte += readLen;
			System.out.println(copyByte + ":" +readLen);
		}
		 long eTime = System.currentTimeMillis();
			 
		in.close();
		out.close();
		System.out.println("복사된 바이트 크기" + copyByte);
		System.out.println("복사에 소요된 시간" + (eTime-sTime));
	}	
}

'JAVA > java 예제' 카테고리의 다른 글

BufferedFileStream 예제  (0) 2020.01.29
DataFilterStream 예제  (0) 2020.01.29
stack queue 예제  (0) 2020.01.29
TreeMap 예제  (0) 2020.01.29
TreeSet 예제  (0) 2020.01.29

+ Recent posts