반응형
자바 기준으로 파일을 복사하는 코드는 아래와 같다.
public static void copy(File src, File dst) throws IOException {
InputStream in = new FileInputStream(src);
try {
OutputStream out = new FileOutputStream(dst);
try {
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
} finally {
out.close();
}
} finally {
in.close();
}
}
API 버전 19부터는 자동 자원 관리 소스를 써서 파일을 다룰 수 있다.
public static void copy(File src, File dst) throws IOException {
try (InputStream in = new FileInputStream(src)) {
try (OutputStream out = new FileOutputStream(dst)) {
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
}
}
}
참고 - https://stackoverflow.com/questions/9292954/how-to-make-a-copy-of-a-file-in-android
반응형
'안드로이드' 카테고리의 다른 글
OKHttp "method GET must not have a request body" 에러 (0) | 2020.02.26 |
---|---|
requestFeature() must be called before adding content 오류 (0) | 2019.12.26 |