package demo.bytestream;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileCopy {

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

		// 지정된 파일을 읽어오는 FileInputStream 생성하기
		FileInputStream in = new FileInputStream("c:/temp/suyoung.jpg");
		
		// 지정된 파일명을 기록하는 FileOutputStream 생성하기
		FileOutputStream out = new FileOutputStream("c:/temp/copy_suyoung.jpg");
		
		int value = 0;
		// InputStream으로부터 1byte씩 읽어온다.
		while ((value = in.read()) != -1) {
			// OutputStream을 사용해서 읽어온 값을 기록한다.
			out.write(value);
		}
		in.close();
		out.close();
	}
}
package demo.bytestream;

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

import org.apache.commons.io.IOUtils;

public class FileCopy2 {

	public static void main(String[] args) throws Exception {
		InputStream in = new FileInputStream("c:/temp/suyoung.jpg");
		OutputStream out = new FileOutputStream("c:/temp/copycopy_suyoung.jpg");
		
		IOUtils.copy(in, out);
	}
}
package demo.bytestream;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;

public class ImageCrawler {

	public static void main(String[] args) throws Exception {
		long startTime = System.currentTimeMillis();
		
		// URL url = new URL("https://img3.daumcdn.net/thumb/R658x0.q70/?fname=https://t1.daumcdn.net/news/201903/26/newsen/20190326101733645qadi.jpg");
		
		// InputStream in = url.openStream();
		// FileOutputStream out = new FileOutputStream("C:/temp/lee.jpg");
		
		FileInputStream in = new FileInputStream("c:/temp/tomcat.exe");
		FileOutputStream out = new FileOutputStream("c:/temp/copy_tomcat.ext");
		
		// 배열에 저장되는 데이터의 개수를 저장하는 변수
		int len = 0;
		// intputStream으로 읽어들인 데이터를 저장하는 배열
		byte[] buf = new byte[1024*8];
		
		// int red(byte[] buf)는 inputStream으로 읽어들인 데이터를
		// 전달받은 배열에 저장하고, 최종적으로 저장된 개수를 반환한다.
		
		while ((len = in.read(buf)) != -1) {
			// void write(byte[] buf, int offset, int len)은 전달받은 배열에 저장된 데이터를
			// offset위치부터 len 개수만큼 기록한다.
			out.write(buf, 0, len);
			
		}
		
		long endTime = System.currentTimeMillis();
		
		//int value = 0;
		//while ((value = in.read()) != -1) {
		//	out.write(value);
		//}
		in.close();
		out.close();
		
	}
}
package demo.bytestream;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;

import org.apache.commons.io.IOUtils;

public class ImageCrawler2 {

	public static void main(String[] args) throws Exception {
		
		URL url = new URL("https://imgnews.pstatic.net/image/346/2019/03/26/0000025295_001_20190326072103336.jpg?type=w647");
		
		InputStream in = url.openStream();
		OutputStream out = new FileOutputStream("c:/temp/미니_소세지.jpeg");
		
		IOUtils.copy(in, out);
	}
}

'자바 > io' 카테고리의 다른 글

writer  (0) 2019.06.10
serialization  (0) 2019.06.10
reader  (0) 2019.06.10
file  (0) 2019.06.10
bridge  (0) 2019.06.10

+ Recent posts