Skip to content

Commit

Permalink
реализовал закрытие файлов в блоке finally и добавил проверку файлово…
Browse files Browse the repository at this point in the history
…го канала на null
  • Loading branch information
LeonidLisin committed Jul 26, 2020
1 parent 4c38b1d commit d6fbfff
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Objects;

@Service
@RequiredArgsConstructor
Expand Down Expand Up @@ -42,14 +43,22 @@ public void save(List<Path> pathes, FullPath fullPath) {
for(Path p: pathes){
boolean isDir = Files.isDirectory(p);
long size = -1;
FileChannel fileChannel = null;
if (!isDir) {
try {
FileChannel fileChannel = FileChannel.open(p);
fileChannel = FileChannel.open(p);
size = fileChannel.size();
fileChannel.close();
} catch (IOException e) {
e.printStackTrace();
}
finally {
Objects.requireNonNull(fileChannel);
try {
fileChannel.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
fileRepository.save(
File.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,21 @@ public FullPathDto obtainDirInfo(FullPath fullPath){
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
long size = 0;
filesCount++;
FileChannel fileChannel = null;
try {
FileChannel fileChannel = FileChannel.open(file);
fileChannel = FileChannel.open(file);
size = fileChannel.size();
fileChannel.close();
} catch (IOException e) {
e.printStackTrace();
}
finally {
Objects.requireNonNull(fileChannel);
try {
fileChannel.close();
} catch (IOException e) {
e.printStackTrace();
}
}
summarySize += size;
fullPathDto.setFilesCount(filesCount);
fullPathDto.setSummarySize(summarySize);
Expand Down

0 comments on commit d6fbfff

Please sign in to comment.