Skip to content

Commit 3908eba

Browse files
committed
lesson 9 видео № 1 "Ввод/вывод":
Класс AbstractFileStorage, метод doUpdate, doGet, doWrite, doRead
1 parent 64ac6ae commit 3908eba

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

src/com/urise/webapp/storage/AbstractFileStorage.java

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,35 @@
33
import com.urise.webapp.exception.StorageException;
44
import com.urise.webapp.model.Resume;
55

6-
import java.io.File;
7-
import java.io.IOException;
6+
import java.io.*;
87
import java.util.ArrayList;
98
import java.util.List;
109
import java.util.Objects;
1110

1211
public abstract class AbstractFileStorage extends AbstractStorage {
1312
private File directory;
13+
protected abstract void doWrite(Resume resume, OutputStream os) throws IOException;
14+
protected abstract Resume doRead(InputStream is)throws IOException;
15+
1416

1517
protected AbstractFileStorage(File directory) {
1618
Objects.requireNonNull(directory,"directory must not null");
17-
if (directory.isDirectory()){
19+
if (!directory.isDirectory()){
1820
throw new IllegalArgumentException(directory.getAbsolutePath() + " is not directory");
1921
}
20-
if (directory.canRead()||directory.canWrite()){
22+
if (!directory.canRead()||!directory.canWrite()){
2123
throw new IllegalArgumentException(directory.getAbsolutePath() + " is not readable/writable");
2224
}
2325
this.directory=directory;
2426
}
2527

2628
@Override
2729
protected void doUpdate(Resume resume, Object searchKey) {
28-
//как doSave только в существующий
29-
File f = (File)searchKey;
30+
File file = (File)searchKey;
3031
try {
31-
doWrite(resume,f);
32+
doWrite(resume, new BufferedOutputStream(new FileOutputStream(file)));
3233
} catch (IOException e) {
33-
throw new StorageException("IO error", f.getName(), e);
34+
throw new StorageException("File write error", resume.getUuid(), e);
3435
}
3536
}
3637

@@ -43,18 +44,22 @@ protected boolean isExist(Object file) {
4344
protected void doSave(Resume resume, Object file) {
4445
File f =((File)file);
4546
try {
47+
4648
f.createNewFile();
47-
doWrite (resume,f);
4849
} catch (IOException e) {
49-
throw new StorageException("IO error", f.getName(), e);
50+
throw new StorageException("Couldn't create file " + f.getAbsolutePath(), f.getName(), e);
5051
}
52+
doUpdate(resume,f);
5153
}
5254

53-
protected abstract void doWrite(Resume resume, File f) throws IOException;
54-
5555
@Override
5656
protected Resume doGet(Object searchKey) {
57-
return doRead((File)searchKey);
57+
File file = (File) searchKey;
58+
try {
59+
return doRead(new BufferedInputStream(new FileInputStream(file)));
60+
} catch (IOException e) {
61+
throw new StorageException("File read error",file.getName(),e);
62+
}
5863
}
5964

6065
@Override
@@ -78,13 +83,11 @@ protected List<Resume> doCopyAll() {
7883
return resumes;
7984
}
8085
for (File f : directory.listFiles()) {
81-
resumes.add(doRead(f));
86+
resumes.add(doGet(f));
8287
}
8388
return resumes;
8489
}
8590

86-
protected abstract Resume doRead(File f);
87-
8891
@Override
8992
public void clear() {
9093
//получить все файлыиз каталога и удалить

0 commit comments

Comments
 (0)