Java Programming

java filecopy

tomato13 2010. 6. 7. 12:09

(http://www.yunsobi.com/blog/406)


import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;


public class StreamCopy {

public void copy(String source, String target) {

File sourceFile = new File(source);

FileInputStream inputStream = null;

FileOutputStream outputStream = null;

try {

inputStream = new FileInputStream(sourceFile);

outputStream = new FileOutputStream(target);

int byteRead = 0;

byte[] buffer = new byte[1024];

while((byteRead = inputStream.read(buffer, 0, 1024))!=-1) {

outputStream.write(buffer, 0, byteRead);

}

} catch(Exception e) {

e.printStackTrace();

} finally {

try {

outputStream.close();

} catch(IOException ioe) {

}

try {

inputStream.close();

} catch(IOException ioe) {

}

}

}

}