[IT]/java
java.byte파일 컨트롤
givemebro
2021. 3. 15. 14:50
반응형

package step4;
import java.io.IOException;
// byte stream을 이용해 이미지 입출력
// FileInputStream < BufferedInputStream
// FileOutputStream < BufferedOuptupStream
public class TestImageService {
public static void main(String[] args) {
String copyImagePath = "C:\\4. kosta215\\iotest4\\iu.jpeg";
String pasteImagePath = "C:\\4. kosta215\\iotest5\\복사본-iu.jpeg";
ImageService service = new ImageService(copyImagePath,pasteImagePath);
try {
service.copyAndPasteImage();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package step4;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class ImageService {
private String copyImagePath;
private String pasteImagePath;
public ImageService(String copyImagePath, String pasteImagePath) {
super();
new File(pasteImagePath).getParentFile().mkdirs();
this.copyImagePath = copyImagePath;
this.pasteImagePath = pasteImagePath;
}
// 이밎 처리를 위해 ByteStream을 사용
// 입력 노드스트림 : FileInputStream, 입력 프로세스스트림 : BufferedInputStream
// 출력 노드스트림 : FileOutputStream, 출력 프로세스스트림 : BufferedOutputStream
public void copyAndPasteImage() throws IOException {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream(copyImagePath));
bos = new BufferedOutputStream(new FileOutputStream(pasteImagePath));
int data = bis.read();
while (data != -1) {
bos.write(data);
data = bis.read();
}
} finally {
if (bis != null) {
bis.close();
}
if (bos != null) {
bis.close();
}
}
}
}
반응형