From 933252a64510a48c6e1ad2d0ab93b89edbae4702 Mon Sep 17 00:00:00 2001 From: binsky Date: Wed, 26 Jan 2022 10:35:35 +0100 Subject: [PATCH] implement readFile storage method --- src/main/java/Storage.java | 25 ++++++++++++++++++++++--- src/test/java/StorageTest.java | 12 ++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/main/java/Storage.java b/src/main/java/Storage.java index fdbaf25..bf2e353 100644 --- a/src/main/java/Storage.java +++ b/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 { @Override @@ -28,4 +27,24 @@ public class Storage implements StorageInterface { } 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; + } } diff --git a/src/test/java/StorageTest.java b/src/test/java/StorageTest.java index 4dddcbd..4f7c595 100644 --- a/src/test/java/StorageTest.java +++ b/src/test/java/StorageTest.java @@ -47,4 +47,16 @@ class StorageTest { File f = new File(testFilePath); 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)); + } } \ No newline at end of file