2014. 11. 4. 16:56 java
java 파일 복사, 삭제, 목록 조회
/**
* 파일 복사
* @param sourceName
* @param targetName
* @throws Exception
*/
private void fileCopy(String sourceName, String targetName) throws Exception {
InputStream inputStream = null;
FileOutputStream fileOutputStream = null;
try {
inputStream = new FileInputStream(new File(sourceName));
fileOutputStream = new FileOutputStream(new File(targetName));
int data = 0;
while ((data = inputStream.read()) != -1) {
fileOutputStream.write(data);
}
fileOutputStream.close();
inputStream.close();
} catch(Exception e) {
throw e;
} finally {
if(fileOutputStream != null) { try { fileOutputStream.close(); } catch(Exception ex) {} }
if(inputStream != null) { try { inputStream.close(); } catch(Exception ex) {} }
}
}
/**
* 파일 목록
* @param fileName
* @return
* @throws Exception
*/
private List<String> getFileList(String fileName) throws Exception {
List<String> list = new ArrayList<String>();
File file = new File(fileName);
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(file));
String line = null;
while( (line = br.readLine()) != null) {
list.add(line);
}
} catch(Exception e) {
e.printStackTrace();
} finally {
if(br != null) try { br.close(); } catch(Exception e) {}
}
return list;
}
/**
* 디렉토리 및 파일 삭제
* @param name
* @throws Exception
*/
private void deleteFile(String name) throws Exception {
File inputFile = new File(name);
if(inputFile.exists()) {
if(inputFile.isDirectory()) {
// 디렉토리이면 하위 파일 및 폴더를 삭제하고, 자신을 삭제
File[] fileList = inputFile.listFiles();
if(fileList != null && fileList.length > 0) {
for(File file : fileList) {
if(file.isDirectory()) deleteFile(file.getAbsolutePath());
else file.delete();
}
}
}
inputFile.delete();
}
}
'java' 카테고리의 다른 글
Java DBCP(DataBase Connection Pool) 사용 (0) | 2014.11.13 |
---|---|
Linux 에서 실행하는 기본 명령들 (0) | 2014.11.06 |
JWT(JSON Web Token) (0) | 2014.10.17 |
PMD로 배우는 올바른 자바 코딩 방법 (0) | 2014.09.16 |
비밀번호 암호화 SHA256, Salt (0) | 2014.08.07 |