Reformat and single liner read* methods

This commit is contained in:
Alex Golub 2023-01-14 19:50:22 +02:00
parent ab5c09a958
commit c34a4ac328
2 changed files with 78 additions and 83 deletions

View File

@ -10,13 +10,14 @@ class ReadFile {
int readFileLineByLine(String filePath) { int readFileLineByLine(String filePath) {
File file = new File(filePath) File file = new File(filePath)
def line, noOfLines = 0; def line, noOfLines = 0;
file.withReader { reader -> file.withReader { reader ->
while ((line = reader.readLine())!=null) while ((line = reader.readLine()) != null) {
{
println "${line}" println "${line}"
noOfLines++ noOfLines++
} }
} }
return noOfLines return noOfLines
} }
@ -26,9 +27,7 @@ class ReadFile {
* @return * @return
*/ */
List<String> readFileInList(String filePath) { List<String> readFileInList(String filePath) {
File file = new File(filePath) return new File(filePath).readLines()
def lines = file.readLines()
return lines
} }
/** /**
@ -37,9 +36,7 @@ class ReadFile {
* @return * @return
*/ */
String readFileString(String filePath) { String readFileString(String filePath) {
File file = new File(filePath) return new File(filePath).text
String fileContent = file.text
return fileContent
} }
/** /**
@ -48,9 +45,7 @@ class ReadFile {
* @return * @return
*/ */
String readFileStringWithCharset(String filePath) { String readFileStringWithCharset(String filePath) {
File file = new File(filePath) return new File(filePath).getText("UTF-8")
String utf8Content = file.getText("UTF-8")
return utf8Content
} }
/** /**
@ -59,9 +54,7 @@ class ReadFile {
* @return * @return
*/ */
byte[] readBinaryFile(String filePath) { byte[] readBinaryFile(String filePath) {
File file = new File(filePath) return new File(filePath).bytes
byte[] binaryContent = file.bytes
return binaryContent
} }
/** /**
@ -85,16 +78,14 @@ class ReadFile {
def array = new File("src/main/resources/fileContent.txt") as String[] def array = new File("src/main/resources/fileContent.txt") as String[]
// eachline // eachline
new File("src/main/resources/fileContent.txt").eachLine { line -> new File("src/main/resources/fileContent.txt").eachLine { println it }
println line
}
//newInputStream with eachLine //newInputStream with eachLine
def is = new File("src/main/resources/fileContent.txt").newInputStream()
is.eachLine { // try-with-resources automatically closes BufferedInputStream resource
println it try (def inputStream = new File("src/main/resources/fileContent.txt").newInputStream()) {
inputStream.eachLine { println it }
} }
is.close()
// withInputStream // withInputStream
new File("src/main/resources/fileContent.txt").withInputStream { stream -> new File("src/main/resources/fileContent.txt").withInputStream { stream ->
@ -103,5 +94,4 @@ class ReadFile {
} }
} }
} }
} }

View File

@ -1,7 +1,6 @@
package com.baeldung.file package com.baeldung.file
import spock.lang.Specification import spock.lang.Specification
import spock.lang.Ignore
class ReadFileUnitTest extends Specification { class ReadFileUnitTest extends Specification {
@ -14,44 +13,49 @@ class ReadFileUnitTest extends Specification {
def 'Should return number of lines in File using ReadFile.readFileLineByLine given filePath'() { def 'Should return number of lines in File using ReadFile.readFileLineByLine given filePath'() {
given: given:
def filePath = "src/main/resources/fileContent.txt" def filePath = "src/main/resources/fileContent.txt"
when: when:
def noOfLines = readFile.readFileLineByLine(filePath) def noOfLines = readFile.readFileLineByLine(filePath)
then: then:
noOfLines noOfLines
noOfLines instanceof Integer noOfLines instanceof Integer
assert noOfLines, 3 noOfLines == 3
} }
def 'Should return File Content in list of lines using ReadFile.readFileInList given filePath'() { def 'Should return File Content in list of lines using ReadFile.readFileInList given filePath'() {
given: given:
def filePath = "src/main/resources/fileContent.txt" def filePath = "src/main/resources/fileContent.txt"
when: when:
def lines = readFile.readFileInList(filePath) def lines = readFile.readFileInList(filePath)
then: then:
lines lines
lines instanceof List<String> lines instanceof List<String>
assert lines.size(), 3 lines.size() == 3
} }
def 'Should return file content in string using ReadFile.readFileString given filePath'() { def 'Should return file content in string using ReadFile.readFileString given filePath'() {
given: given:
def filePath = "src/main/resources/fileContent.txt" def filePath = "src/main/resources/fileContent.txt"
when: when:
def fileContent = readFile.readFileString(filePath) def fileContent = readFile.readFileString(filePath)
then: then:
fileContent fileContent
fileContent instanceof String fileContent instanceof String
fileContent.contains("""Line 1 : Hello World!!! fileContent.contains(["Line 1 : Hello World!!!", "Line 2 : This is a file content.", "Line 3 : String content"].join("\r\n"))
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'() { def 'Should return UTF-8 encoded file content in string using ReadFile.readFileStringWithCharset given filePath'() {
given: given:
def filePath = "src/main/resources/utf8Content.html" def filePath = "src/main/resources/utf8Content.html"
when: when:
def encodedContent = readFile.readFileStringWithCharset(filePath) def encodedContent = readFile.readFileStringWithCharset(filePath)
then: then:
encodedContent encodedContent
encodedContent instanceof String encodedContent instanceof String
@ -60,12 +64,13 @@ Line 3 : String content""")
def 'Should return binary file content in byte array using ReadFile.readBinaryFile given filePath'() { def 'Should return binary file content in byte array using ReadFile.readBinaryFile given filePath'() {
given: given:
def filePath = "src/main/resources/sample.png" def filePath = "src/main/resources/sample.png"
when: when:
def binaryContent = readFile.readBinaryFile(filePath) def binaryContent = readFile.readBinaryFile(filePath)
then: then:
binaryContent binaryContent
binaryContent instanceof byte[] binaryContent instanceof byte[]
binaryContent.length == 329 binaryContent.length == 329
} }
} }