728x90
# StandardCopyOption.REPLACE_EXISTING
Files.copy(file.getInputStream(), targetLocation, StandardCopyOption.REPLACE_EXISTING);
StandardCopyOption.REPLACE_EXISTING
똑같은 파일(이름 같은것)이 있을때 덮어쓴다.
# Files.createDirectory
Files.createDirectories(this.fileLocation);
Files.createDirectory(path);
path에 해당하는 디렉토리를 만듬.
# Files.exists
Path path = Paths.get("/tmp/test.txt");
boolean exists = Files.exists(path, LinkOption.NOFOLLOW_LINKS);
주어진 path에 해당하는 파일이 파일시스템 안에 존재하는지 확인함.
# Files.copy
Path source = Paths.get("/tmp/sourceFile");
Path destination = Paths.get("/tmp/destinationFile");
try {
Files.copy(source, destination);
} catch(FileAlreadyExistsException e) {
// 파일이 이미 존재하는 경우
} catch (IOException e) {
// 뭔가 다른 문제가 발생한 경우
}
source에 해당하는 파일을 destination 폴더에 복사한다.
# Files.move
Path source = Paths.get("/tmp/sourceFile");
Path destination = Paths.get("/tmp/destinationFile");
try {
Files.move(source, destination, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
// 실패..
}
파일의 위치를 이동함.
터미널에서 mv 명령과 같음.
# Files.delete(path)
Path path = Paths.get("/tmp/sourceFile");
try {
Files.delete(path);
} catch (IOException e) {
// 실패..
}
파일을 삭제함.
# Files.wlakFileTree()
public interface FileVisitor {
// 디렉토리를 방문하기 전에 호출되는 메소드
public FileVisitResult preVisitDirectory(
Path dir, BasicFileAttributes attrs) throws IOException;
// 파일에 다다랐을 때 호출되는 메소드
public FileVisitResult visitFile(
Path file, BasicFileAttributes attrs) throws IOException;
// 파일에 다다르지 못 했을 때 호출되는 메소드 (권한문제 등)
public FileVisitResult visitFileFailed(
Path file, IOException exc) throws IOException;
// 디렉토리를 방문하고 나서 호출되는 메소드
public FileVisitResult postVisitDirectory(
Path dir, IOException exc) throws IOException {
}
'개발 > JAVA' 카테고리의 다른 글
[JAVA] Generic 정의 (0) | 2022.11.29 |
---|---|
Java 대용량 데이터 DB 처리 방법, batch (0) | 2022.11.24 |
NestedConvertHelper helper에서 값 안받아와져서 변경 해준것. (0) | 2022.11.14 |
return 필요 없으면 void로 하기 (0) | 2022.11.14 |
private final 변수가 다른 곳에서 먼저 생성자로 사용하고 있을 때? (0) | 2022.10.31 |