Created
June 9, 2016 09:00
-
-
Save ksu3101/b691f2885afdb289070675c99fbcf2f6 to your computer and use it in GitHub Desktop.
File delete method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 파일 하나를 복사 한다. AsyncTask등에 태워서 처리 할 것 | |
* | |
* @param from 복사할 파일의 절대 경로 혹은 객체 | |
* @param target 저장할 파일의 경로 혹은 객체 | |
* @param <T> String or File instance | |
* @return true일 경우 파일 복사가 성공적으로 수행 됨. | |
*/ | |
public static <T> boolean fileCopy(T from, T target) { | |
if (from != null) { | |
File fromFile = null; | |
if (from instanceof String) { | |
fromFile = new File((String) from); | |
} | |
else if (from instanceof File) { | |
fromFile = (File) from; | |
} | |
else { | |
log.w(TAG, "File이나 String파일 절대 경로만 가능 합니다."); | |
return false; | |
} | |
if (fromFile.exists()) { | |
File targetFile = null; | |
if (target != null) { | |
if (target instanceof String) { | |
targetFile = new File((String) target); | |
} | |
else if (target instanceof File) { | |
targetFile = (File) target; | |
} | |
else { | |
log.w(TAG, "File이나 String파일 절대 경로만 가능 합니다."); | |
return false; | |
} | |
try { | |
FileInputStream fis = new FileInputStream(fromFile); | |
FileOutputStream fos = new FileOutputStream(targetFile); | |
int readCount = 0; | |
byte[] buffer = new byte[1024]; | |
while ((readCount = fis.read(buffer, 0, 1024)) != -1) { | |
fos.write(buffer, 0, readCount); | |
} | |
fos.close(); | |
fis.close(); | |
} catch (IOException ioe) { | |
log.e(TAG, ioe.getMessage()); | |
return false; | |
} | |
if (targetFile.length() > 0) { | |
return true; | |
} | |
} | |
} | |
} | |
return false; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 파일 하나를 삭제 한다. | |
* | |
* @param file 삭제할 파일의 경로 혹은 파일 객체 | |
* @param <T> String or File instance | |
* @return true일 경우 파일 삭제가 성공적으로 수행 됨. | |
*/ | |
public static <T> boolean fileDelete(T file) { | |
if (file != null) { | |
File targetFile = null; | |
if (file instanceof String) { | |
targetFile = new File((String) file); | |
} | |
else if (file instanceof File) { | |
targetFile = (File) file; | |
} | |
else { | |
log.w(TAG, "File이나 String파일 절대 경로만 가능 합니다."); | |
return false; | |
} | |
return targetFile.delete(); | |
} | |
return false; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 파일 하나를 이동 한다. AsyncTask등에 태워서 사용 할 것 | |
* | |
* @param fromPath 이동할 파일의 절대 경로 | |
* @param target 이동할 경로 혹은 파일 객체 | |
* @param <T> String or File instance | |
* @return true일 경우 이동이 성공적으로 수행 됨. | |
*/ | |
public static <T> boolean fileMove(String fromPath, T target) { | |
if (!TextUtils.isEmpty(fromPath)) { | |
if (target != null) { | |
File targetFile = null; | |
if (target instanceof String) { | |
targetFile = new File((String) target); | |
} | |
else if (target instanceof File) { | |
targetFile = (File) target; | |
} | |
else { | |
// Uri는 getPathFromUri()메소드를 활용 할 것. | |
log.w(TAG, "File이나 String파일 절대 경로만 가능 합니다."); | |
return false; | |
} | |
File fromFile = new File(fromPath); | |
if (fromFile.exists()) { | |
if (!fromFile.renameTo(targetFile)) { | |
// File의 renameTo는 파일을 정상적으로 이동시키지 못하는 경우가 있다. | |
// 또한 이에 대한 예외조차 없다. | |
try { | |
// 기존 파일을 복사 하고 난 뒤 원본을 삭제 한다. | |
FileInputStream fis = new FileInputStream(fromFile); | |
FileOutputStream fos = new FileOutputStream(targetFile); | |
byte[] buf = new byte[1024]; | |
int read = 0; | |
while ((read = fis.read(buf, 0, buf.length)) != -1) { | |
fos.write(buf, 0, read); | |
} | |
fis.close(); | |
fos.close(); | |
return fromFile.delete(); | |
} catch (FileNotFoundException fnfe) { | |
log.e(TAG, fnfe.getMessage()); | |
return false; | |
} catch (IOException ioe) { | |
log.e(TAG, ioe.getMessage()); | |
return false; | |
} | |
} | |
else { | |
return true; | |
} | |
} | |
} | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment