From ac53498b767eaa5052933373a823a58365263583 Mon Sep 17 00:00:00 2001 From: Anshul Bansal Date: Sun, 27 Jan 2019 15:40:36 +0200 Subject: [PATCH 1/7] BAEL-2659 - Reading a file in Groovy --- .../groovy/com/baeldung/file/ReadFile.groovy | 61 +++++++++++++++++++ .../src/main/resources/fileContent.txt | 3 + .../com/baeldung/file/ReadFileUnitTest.groovy | 58 ++++++++++++++++++ 3 files changed, 122 insertions(+) create mode 100644 core-groovy/src/main/groovy/com/baeldung/file/ReadFile.groovy create mode 100644 core-groovy/src/main/resources/fileContent.txt create mode 100644 core-groovy/src/test/groovy/com/baeldung/file/ReadFileUnitTest.groovy diff --git a/core-groovy/src/main/groovy/com/baeldung/file/ReadFile.groovy b/core-groovy/src/main/groovy/com/baeldung/file/ReadFile.groovy new file mode 100644 index 0000000000..fca5bde06f --- /dev/null +++ b/core-groovy/src/main/groovy/com/baeldung/file/ReadFile.groovy @@ -0,0 +1,61 @@ +package com.baeldung.file + +class ReadFile { + + /** + * reads file content in string using File.text + * @param filePath + * @return + */ + String readFileString(String filePath) { + File file = new File(filePath) + String fileContent = file.text + return fileContent + } + + /** + * reads file content in string with encoding using File.getText + * @param filePath + * @return + */ + String readFileStringWithCharset(String filePath) { + File file = new File(filePath) + String utf8Content = file.getText("UTF-8") + return utf8Content + } + + /** + * reads file content line by line using withReader and reader.readLine + * @param filePath + * @return + */ + int readFileLineByLine(String filePath) { + File file = new File(filePath) + def line, noOfLines = 0; + file.withReader { reader -> + while ((line = reader.readLine())!=null) + { + println "${line}" + noOfLines++ + } + } + return noOfLines + } + + /** + * reads file content in list of lines + * @param filePath + * @return + */ + List readFileInList(String filePath) { + File file = new File(filePath) + def lines = file.readLines() + return lines + } + + public static void main(String[] args) { + def file = new File("../../src") + println file.directorySize + } + +} \ No newline at end of file diff --git a/core-groovy/src/main/resources/fileContent.txt b/core-groovy/src/main/resources/fileContent.txt new file mode 100644 index 0000000000..643e4af3de --- /dev/null +++ b/core-groovy/src/main/resources/fileContent.txt @@ -0,0 +1,3 @@ +Line 1 : Hello World!!! +Line 2 : This is a file content. +Line 3 : String content diff --git a/core-groovy/src/test/groovy/com/baeldung/file/ReadFileUnitTest.groovy b/core-groovy/src/test/groovy/com/baeldung/file/ReadFileUnitTest.groovy new file mode 100644 index 0000000000..c90cc8b960 --- /dev/null +++ b/core-groovy/src/test/groovy/com/baeldung/file/ReadFileUnitTest.groovy @@ -0,0 +1,58 @@ +package com.baeldung.file + +import spock.lang.Specification + +class ReadFileUnitTest extends Specification { + + ReadFile readFile + + void setup () { + readFile = new ReadFile() + } + + def 'Should return file content in string using ReadFile.readFileString given filePath' () { + given: + def filePath = "src/main/resources/fileContent.txt" + when: + def fileContent = readFile.readFileString(filePath) + then: + fileContent + fileContent instanceof String + fileContent.contains("""Line 1 : Hello World!!! +Line 2 : This is a file content. +Line 3 : String content""") + + } + + def 'Should return UTF-8 encoded file content in string using ReadFile.readFileStringWithCharset given filePath' () { + given: + def filePath = "src/main/resources/fileContent.txt" + when: + def noOfLines = readFile.readFileStringWithCharset(filePath) + then: + noOfLines + noOfLines instanceof String + } + + def 'Should return number of lines in File using ReadFile.readFileLineByLine given filePath' () { + given: + def filePath = "src/main/resources/fileContent.txt" + when: + def noOfLines = readFile.readFileLineByLine(filePath) + then: + noOfLines + noOfLines instanceof Integer + assert noOfLines, 3 + } + + def 'Should return File Content in list of lines using ReadFile.readFileInList given filePath' () { + given: + def filePath = "src/main/resources/fileContent.txt" + when: + def lines = readFile.readFileInList(filePath) + then: + lines + lines instanceof List + assert lines.size(), 3 + } +} \ No newline at end of file From f22d1073d19416ba5d327149b316babebb8b4b47 Mon Sep 17 00:00:00 2001 From: Anshul Bansal Date: Sun, 27 Jan 2019 22:47:27 +0200 Subject: [PATCH 2/7] BAEL-2659 - Reading a file in Groovy - changed order of functions --- .../groovy/com/baeldung/file/ReadFile.groovy | 48 +++++++++---------- .../com/baeldung/file/ReadFileUnitTest.groovy | 44 ++++++++--------- 2 files changed, 44 insertions(+), 48 deletions(-) diff --git a/core-groovy/src/main/groovy/com/baeldung/file/ReadFile.groovy b/core-groovy/src/main/groovy/com/baeldung/file/ReadFile.groovy index fca5bde06f..73208a52c8 100644 --- a/core-groovy/src/main/groovy/com/baeldung/file/ReadFile.groovy +++ b/core-groovy/src/main/groovy/com/baeldung/file/ReadFile.groovy @@ -2,28 +2,6 @@ package com.baeldung.file class ReadFile { - /** - * reads file content in string using File.text - * @param filePath - * @return - */ - String readFileString(String filePath) { - File file = new File(filePath) - String fileContent = file.text - return fileContent - } - - /** - * reads file content in string with encoding using File.getText - * @param filePath - * @return - */ - String readFileStringWithCharset(String filePath) { - File file = new File(filePath) - String utf8Content = file.getText("UTF-8") - return utf8Content - } - /** * reads file content line by line using withReader and reader.readLine * @param filePath @@ -52,10 +30,28 @@ class ReadFile { def lines = file.readLines() return lines } - - public static void main(String[] args) { - def file = new File("../../src") - println file.directorySize + + /** + * reads file content in string using File.text + * @param filePath + * @return + */ + String readFileString(String filePath) { + File file = new File(filePath) + String fileContent = file.text + return fileContent } + /** + * reads file content in string with encoding using File.getText + * @param filePath + * @return + */ + String readFileStringWithCharset(String filePath) { + File file = new File(filePath) + String utf8Content = file.getText("UTF-8") + return utf8Content + } + + } \ No newline at end of file diff --git a/core-groovy/src/test/groovy/com/baeldung/file/ReadFileUnitTest.groovy b/core-groovy/src/test/groovy/com/baeldung/file/ReadFileUnitTest.groovy index c90cc8b960..105a8e157f 100644 --- a/core-groovy/src/test/groovy/com/baeldung/file/ReadFileUnitTest.groovy +++ b/core-groovy/src/test/groovy/com/baeldung/file/ReadFileUnitTest.groovy @@ -10,6 +10,28 @@ class ReadFileUnitTest extends Specification { readFile = new ReadFile() } + def 'Should return number of lines in File using ReadFile.readFileLineByLine given filePath' () { + given: + def filePath = "src/main/resources/fileContent.txt" + when: + def noOfLines = readFile.readFileLineByLine(filePath) + then: + noOfLines + noOfLines instanceof Integer + assert noOfLines, 3 + } + + def 'Should return File Content in list of lines using ReadFile.readFileInList given filePath' () { + given: + def filePath = "src/main/resources/fileContent.txt" + when: + def lines = readFile.readFileInList(filePath) + then: + lines + lines instanceof List + assert lines.size(), 3 + } + def 'Should return file content in string using ReadFile.readFileString given filePath' () { given: def filePath = "src/main/resources/fileContent.txt" @@ -33,26 +55,4 @@ Line 3 : String content""") noOfLines noOfLines instanceof String } - - def 'Should return number of lines in File using ReadFile.readFileLineByLine given filePath' () { - given: - def filePath = "src/main/resources/fileContent.txt" - when: - def noOfLines = readFile.readFileLineByLine(filePath) - then: - noOfLines - noOfLines instanceof Integer - assert noOfLines, 3 - } - - def 'Should return File Content in list of lines using ReadFile.readFileInList given filePath' () { - given: - def filePath = "src/main/resources/fileContent.txt" - when: - def lines = readFile.readFileInList(filePath) - then: - lines - lines instanceof List - assert lines.size(), 3 - } } \ No newline at end of file From 1f4d1839f44232312c289e30b6869420dd558520 Mon Sep 17 00:00:00 2001 From: Anshul Bansal Date: Tue, 29 Jan 2019 17:27:52 +0200 Subject: [PATCH 3/7] BAEL-2659 - Reading a file in Groovy - added more examples and unit tests --- .../groovy/com/baeldung/file/ReadFile.groovy | 36 ++++++++++++++++++ core-groovy/src/main/resources/sample.png | Bin 0 -> 329 bytes .../src/main/resources/utf8Content.html | 5 +++ .../com/baeldung/file/ReadFileUnitTest.groovy | 12 +++++- 4 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 core-groovy/src/main/resources/sample.png create mode 100644 core-groovy/src/main/resources/utf8Content.html diff --git a/core-groovy/src/main/groovy/com/baeldung/file/ReadFile.groovy b/core-groovy/src/main/groovy/com/baeldung/file/ReadFile.groovy index 73208a52c8..38bf189e63 100644 --- a/core-groovy/src/main/groovy/com/baeldung/file/ReadFile.groovy +++ b/core-groovy/src/main/groovy/com/baeldung/file/ReadFile.groovy @@ -53,5 +53,41 @@ class ReadFile { return utf8Content } + /** + * reads content of binary file and returns byte array + * @param filePath + * @return + */ + byte[] readBinaryFile(String filePath) { + File file = new File(filePath) + byte[] binaryContent = file.bytes + return binaryContent + } + + /** + * More Examples of reading a file + * @return + */ + def moreExamples() { + def list = new File("src/main/resources/fileContent.txt").collect {it} + + def array = new File("src/main/resources/fileContent.txt") as String[] + + new File("src/main/resources/fileContent.txt").eachLine { line -> + println line + } + + def is = new File("src/main/resources/fileContent.txt").newInputStream() + is.eachLine { + println it + } + is.close() + + new File("src/main/resources/fileContent.txt").withInputStream { stream -> + stream.eachLine { line -> + println line + } + } + } } \ No newline at end of file diff --git a/core-groovy/src/main/resources/sample.png b/core-groovy/src/main/resources/sample.png new file mode 100644 index 0000000000000000000000000000000000000000..7d473430b7bec514f7de12f5769fe7c5859e8c5d GIT binary patch literal 329 zcmeAS@N?(olHy`uVBq!ia0vp^JRr;gBp8b2n5}^nQC}X^4DKU-G|w_t}fLBA)Suv#nrW z!^h2QnY_`l!BOq-UXEX{m2up>JTQkX)2m zTvF+fTUlI^nXH#utd~++ke^qgmzgTe~DWM4ffP81J literal 0 HcmV?d00001 diff --git a/core-groovy/src/main/resources/utf8Content.html b/core-groovy/src/main/resources/utf8Content.html new file mode 100644 index 0000000000..9d941200b1 --- /dev/null +++ b/core-groovy/src/main/resources/utf8Content.html @@ -0,0 +1,5 @@ +ᚠᛇᚻ᛫ᛒᛦᚦ᛫ᚠᚱᚩᚠᚢᚱ᛫ᚠᛁᚱᚪ᛫ᚷᛖᚻᚹᛦᛚᚳᚢᛗ +ᛋᚳᛖᚪᛚ᛫ᚦᛖᚪᚻ᛫ᛗᚪᚾᚾᚪ᛫ᚷᛖᚻᚹᛦᛚᚳ᛫ᛗᛁᚳᛚᚢᚾ᛫ᚻᛦᛏ᛫ᛞᚫᛚᚪᚾ +ᚷᛁᚠ᛫ᚻᛖ᛫ᚹᛁᛚᛖ᛫ᚠᚩᚱ᛫ᛞᚱᛁᚻᛏᚾᛖ᛫ᛞᚩᛗᛖᛋ᛫ᚻᛚᛇᛏᚪᚾ + +¥ · £ · € · $ · ¢ · ₡ · ₢ · ₣ · ₤ · ₥ · ₦ · ₧ · ₨ · ₩ · ₪ · ₫ · ₭ · ₮ · ₯ · ₹ \ No newline at end of file diff --git a/core-groovy/src/test/groovy/com/baeldung/file/ReadFileUnitTest.groovy b/core-groovy/src/test/groovy/com/baeldung/file/ReadFileUnitTest.groovy index 105a8e157f..5fc1409276 100644 --- a/core-groovy/src/test/groovy/com/baeldung/file/ReadFileUnitTest.groovy +++ b/core-groovy/src/test/groovy/com/baeldung/file/ReadFileUnitTest.groovy @@ -48,11 +48,21 @@ Line 3 : String content""") def 'Should return UTF-8 encoded file content in string using ReadFile.readFileStringWithCharset given filePath' () { given: - def filePath = "src/main/resources/fileContent.txt" + def filePath = "src/main/resources/utf8Content.html" when: def noOfLines = readFile.readFileStringWithCharset(filePath) then: noOfLines noOfLines instanceof String } + + def 'Should return binary file content in byte arry using ReadFile.readBinaryFile given filePath' () { + given: + def filePath = "src/main/resources/sample.png" + when: + def noOfLines = readFile.readBinaryFile(filePath) + then: + noOfLines + noOfLines instanceof byte[] + } } \ No newline at end of file From 50a8870d4f95175e409b51972fc31ae014bb1a2f Mon Sep 17 00:00:00 2001 From: Anshul Bansal Date: Wed, 30 Jan 2019 13:46:56 +0200 Subject: [PATCH 4/7] BAEL-2659 - Reading a file in Groovy - added more example --- .../src/main/groovy/com/baeldung/file/ReadFile.groovy | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/core-groovy/src/main/groovy/com/baeldung/file/ReadFile.groovy b/core-groovy/src/main/groovy/com/baeldung/file/ReadFile.groovy index 38bf189e63..828f702e82 100644 --- a/core-groovy/src/main/groovy/com/baeldung/file/ReadFile.groovy +++ b/core-groovy/src/main/groovy/com/baeldung/file/ReadFile.groovy @@ -69,6 +69,14 @@ class ReadFile { * @return */ def moreExamples() { + + new File("src/main/resources/utf8Content.html").withReader('UTF-8') { reader -> + def line + while ((line = reader.readLine())!=null) { + println "${line}" + } + } + def list = new File("src/main/resources/fileContent.txt").collect {it} def array = new File("src/main/resources/fileContent.txt") as String[] From b48f77cf6e61afb2990f52a9fe515813aa0f8f3a Mon Sep 17 00:00:00 2001 From: Anshul Bansal Date: Thu, 31 Jan 2019 12:46:25 +0200 Subject: [PATCH 5/7] BAEL-2659 - Reading a file in Groovy - corrected variable names --- .../main/groovy/com/baeldung/file/ReadFile.groovy | 8 +++++++- core-groovy/src/main/resources/utf8Content.html | 4 +--- .../com/baeldung/file/ReadFileUnitTest.groovy | 14 ++++++++------ 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/core-groovy/src/main/groovy/com/baeldung/file/ReadFile.groovy b/core-groovy/src/main/groovy/com/baeldung/file/ReadFile.groovy index 828f702e82..4239fa534c 100644 --- a/core-groovy/src/main/groovy/com/baeldung/file/ReadFile.groovy +++ b/core-groovy/src/main/groovy/com/baeldung/file/ReadFile.groovy @@ -70,6 +70,7 @@ class ReadFile { */ def moreExamples() { + //with reader with utf-8 new File("src/main/resources/utf8Content.html").withReader('UTF-8') { reader -> def line while ((line = reader.readLine())!=null) { @@ -77,20 +78,25 @@ class ReadFile { } } + //collect api def list = new File("src/main/resources/fileContent.txt").collect {it} + //as operator def array = new File("src/main/resources/fileContent.txt") as String[] - + + //eachline new File("src/main/resources/fileContent.txt").eachLine { line -> println line } + //newInputStream with eachLine def is = new File("src/main/resources/fileContent.txt").newInputStream() is.eachLine { println it } is.close() + //withInputStream new File("src/main/resources/fileContent.txt").withInputStream { stream -> stream.eachLine { line -> println line diff --git a/core-groovy/src/main/resources/utf8Content.html b/core-groovy/src/main/resources/utf8Content.html index 9d941200b1..873a75b8d0 100644 --- a/core-groovy/src/main/resources/utf8Content.html +++ b/core-groovy/src/main/resources/utf8Content.html @@ -1,5 +1,3 @@ ᚠᛇᚻ᛫ᛒᛦᚦ᛫ᚠᚱᚩᚠᚢᚱ᛫ᚠᛁᚱᚪ᛫ᚷᛖᚻᚹᛦᛚᚳᚢᛗ ᛋᚳᛖᚪᛚ᛫ᚦᛖᚪᚻ᛫ᛗᚪᚾᚾᚪ᛫ᚷᛖᚻᚹᛦᛚᚳ᛫ᛗᛁᚳᛚᚢᚾ᛫ᚻᛦᛏ᛫ᛞᚫᛚᚪᚾ -ᚷᛁᚠ᛫ᚻᛖ᛫ᚹᛁᛚᛖ᛫ᚠᚩᚱ᛫ᛞᚱᛁᚻᛏᚾᛖ᛫ᛞᚩᛗᛖᛋ᛫ᚻᛚᛇᛏᚪᚾ - -¥ · £ · € · $ · ¢ · ₡ · ₢ · ₣ · ₤ · ₥ · ₦ · ₧ · ₨ · ₩ · ₪ · ₫ · ₭ · ₮ · ₯ · ₹ \ No newline at end of file +ᚷᛁᚠ᛫ᚻᛖ᛫ᚹᛁᛚᛖ᛫ᚠᚩᚱ᛫ᛞᚱᛁᚻᛏᚾᛖ᛫ᛞᚩᛗᛖᛋ᛫ᚻᛚᛇᛏᚪᚾ \ No newline at end of file diff --git a/core-groovy/src/test/groovy/com/baeldung/file/ReadFileUnitTest.groovy b/core-groovy/src/test/groovy/com/baeldung/file/ReadFileUnitTest.groovy index 5fc1409276..0d0e2aed1a 100644 --- a/core-groovy/src/test/groovy/com/baeldung/file/ReadFileUnitTest.groovy +++ b/core-groovy/src/test/groovy/com/baeldung/file/ReadFileUnitTest.groovy @@ -50,19 +50,21 @@ Line 3 : String content""") given: def filePath = "src/main/resources/utf8Content.html" when: - def noOfLines = readFile.readFileStringWithCharset(filePath) + def encodedContent = readFile.readFileStringWithCharset(filePath) then: - noOfLines - noOfLines instanceof String + encodedContent + encodedContent instanceof String } def 'Should return binary file content in byte arry using ReadFile.readBinaryFile given filePath' () { given: def filePath = "src/main/resources/sample.png" when: - def noOfLines = readFile.readBinaryFile(filePath) + def binaryContent = readFile.readBinaryFile(filePath) then: - noOfLines - noOfLines instanceof byte[] + binaryContent + binaryContent instanceof byte[] + binaryContent.length == 329 } + } \ No newline at end of file From 4260b353c5e992d8556eefaa3287e05d278b2b43 Mon Sep 17 00:00:00 2001 From: Anshul Bansal Date: Sun, 3 Feb 2019 13:53:53 +0200 Subject: [PATCH 6/7] BAEL-2659 - Reading a file in Groovy - html content --- core-groovy/src/main/resources/utf8Content.html | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/core-groovy/src/main/resources/utf8Content.html b/core-groovy/src/main/resources/utf8Content.html index 873a75b8d0..61ff338f6c 100644 --- a/core-groovy/src/main/resources/utf8Content.html +++ b/core-groovy/src/main/resources/utf8Content.html @@ -1,3 +1,12 @@ + + + + +
 ᚠᛇᚻ᛫ᛒᛦᚦ᛫ᚠᚱᚩᚠᚢᚱ᛫ᚠᛁᚱᚪ᛫ᚷᛖᚻᚹᛦᛚᚳᚢᛗ
 ᛋᚳᛖᚪᛚ᛫ᚦᛖᚪᚻ᛫ᛗᚪᚾᚾᚪ᛫ᚷᛖᚻᚹᛦᛚᚳ᛫ᛗᛁᚳᛚᚢᚾ᛫ᚻᛦᛏ᛫ᛞᚫᛚᚪᚾ
-ᚷᛁᚠ᛫ᚻᛖ᛫ᚹᛁᛚᛖ᛫ᚠᚩᚱ᛫ᛞᚱᛁᚻᛏᚾᛖ᛫ᛞᚩᛗᛖᛋ᛫ᚻᛚᛇᛏᚪᚾ
\ No newline at end of file
+ᚷᛁᚠ᛫ᚻᛖ᛫ᚹᛁᛚᛖ᛫ᚠᚩᚱ᛫ᛞᚱᛁᚻᛏᚾᛖ᛫ᛞᚩᛗᛖᛋ᛫ᚻᛚᛇᛏᚪᚾ
+
+ + \ No newline at end of file From 6a8e295febf1e6ecb98b59f650ac13226eaabf9f Mon Sep 17 00:00:00 2001 From: Anshul Bansal Date: Fri, 8 Feb 2019 08:53:29 +0200 Subject: [PATCH 7/7] BAEL-2659 - Reading a file in Groovy - typo corrected --- .../src/test/groovy/com/baeldung/file/ReadFileUnitTest.groovy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core-groovy/src/test/groovy/com/baeldung/file/ReadFileUnitTest.groovy b/core-groovy/src/test/groovy/com/baeldung/file/ReadFileUnitTest.groovy index 0d0e2aed1a..a479c265c4 100644 --- a/core-groovy/src/test/groovy/com/baeldung/file/ReadFileUnitTest.groovy +++ b/core-groovy/src/test/groovy/com/baeldung/file/ReadFileUnitTest.groovy @@ -56,7 +56,7 @@ Line 3 : String content""") encodedContent instanceof String } - def 'Should return binary file content in byte arry using ReadFile.readBinaryFile given filePath' () { + def 'Should return binary file content in byte array using ReadFile.readBinaryFile given filePath' () { given: def filePath = "src/main/resources/sample.png" when: