Programming/Java
Input Output Stream
SharingWorld
2018. 4. 9. 15:21
// FILE Stream
// Network Stream
// => InputStream
// => OutputStream
import org.junit.Test;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Program_0308 {
public static final String SRC_PATH = "/Users/ourguide/Downloads/1.pdf";
public static final String DEST_PATH = "/Users/ourguide/Desktop/4.pdf";
public static void fileCopy(String src, String dest) throws Exception {
FileInputStream fis = new FileInputStream(src);
FileOutputStream fos = new FileOutputStream(dest);
while (true) {
int data = fis.read();
if (data == -1)
break;
fos.write(data);
}
}
// Java Stream 기반의 처리 방식
// => 각각의 기능을 별도의 클래스로 제공하고 있다.
// => 자바의 파일 IO는 성능에 문제가 있다.
// : NIO API
public static void fileCopy2(String src, String dest) throws Exception {
FileInputStream fis = new FileInputStream(src);
BufferedInputStream bis = new BufferedInputStream(fis);
FileOutputStream fos = new FileOutputStream(dest);
BufferedOutputStream bos = new BufferedOutputStream(fos);
while (true) {
int data = bis.read();
if (data == -1)
break;
bos.write(data);
}
}
@Test
public void copyTest1() throws Exception {
fileCopy(SRC_PATH, DEST_PATH);
}
@Test
public void copyTest2() throws Exception {
fileCopy2(SRC_PATH, DEST_PATH);
}
@Test
public void copyTest3() throws Exception {
Path spath = Paths.get(SRC_PATH);
Path dpath = Paths.get(DEST_PATH);
Files.copy(spath, dpath);
}
}