Browse Source

implement readFile storage method

feature-add-storage-class
binsky 3 years ago
parent
commit
933252a645
  1. 25
      src/main/java/Storage.java
  2. 12
      src/test/java/StorageTest.java

25
src/main/java/Storage.java

@ -1,6 +1,5 @@
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.*;
import java.util.Scanner;
public class Storage implements StorageInterface { public class Storage implements StorageInterface {
@Override @Override
@ -28,4 +27,24 @@ public class Storage implements StorageInterface {
} }
return false; return false;
} }
public String readFile(String path) {
if (!path.isEmpty()) {
StringBuilder data = new StringBuilder();
try {
File f = new File(path);
Scanner myReader = new Scanner(f);
while (myReader.hasNextLine()) {
data.append(myReader.nextLine());
}
myReader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
return data.toString();
}
return null;
}
} }

12
src/test/java/StorageTest.java

@ -47,4 +47,16 @@ class StorageTest {
File f = new File(testFilePath); File f = new File(testFilePath);
assertTrue(f.isFile()); assertTrue(f.isFile());
} }
@Test
void readFile() {
String content = "test";
File f = new File(testFilePath);
if (!f.isFile()) {
assertTrue(storage.writeFile(testFilePath, content));
}
assertEquals(content, storage.readFile(testFilePath));
}
} }
Loading…
Cancel
Save