From b48f77cf6e61afb2990f52a9fe515813aa0f8f3a Mon Sep 17 00:00:00 2001 From: Anshul Bansal Date: Thu, 31 Jan 2019 12:46:25 +0200 Subject: [PATCH] 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