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) {
File file = new File(filePath)
def line, noOfLines = 0;
file.withReader { reader ->
while ((line = reader.readLine())!=null)
{
while ((line = reader.readLine()) != null) {
println "${line}"
noOfLines++
}
}
return noOfLines
}
@ -26,9 +27,7 @@ class ReadFile {
* @return
*/
List<String> readFileInList(String filePath) {
File file = new File(filePath)
def lines = file.readLines()
return lines
return new File(filePath).readLines()
}
/**
@ -37,9 +36,7 @@ class ReadFile {
* @return
*/
String readFileString(String filePath) {
File file = new File(filePath)
String fileContent = file.text
return fileContent
return new File(filePath).text
}
/**
@ -48,9 +45,7 @@ class ReadFile {
* @return
*/
String readFileStringWithCharset(String filePath) {
File file = new File(filePath)
String utf8Content = file.getText("UTF-8")
return utf8Content
return new File(filePath).getText("UTF-8")
}
/**
@ -59,9 +54,7 @@ class ReadFile {
* @return
*/
byte[] readBinaryFile(String filePath) {
File file = new File(filePath)
byte[] binaryContent = file.bytes
return binaryContent
return new File(filePath).bytes
}
/**
@ -73,35 +66,32 @@ class ReadFile {
//with reader with utf-8
new File("src/main/resources/utf8Content.html").withReader('UTF-8') { reader ->
def line
while ((line = reader.readLine())!=null) {
while ((line = reader.readLine()) != null) {
println "${line}"
}
}
//collect api
def list = new File("src/main/resources/fileContent.txt").collect {it}
// collect api
def list = new File("src/main/resources/fileContent.txt").collect { it }
//as operator
// 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
}
// eachline
new File("src/main/resources/fileContent.txt").eachLine { println it }
//newInputStream with eachLine
def is = new File("src/main/resources/fileContent.txt").newInputStream()
is.eachLine {
println it
}
is.close()
//withInputStream
// try-with-resources automatically closes BufferedInputStream resource
try (def inputStream = new File("src/main/resources/fileContent.txt").newInputStream()) {
inputStream.eachLine { println it }
}
// withInputStream
new File("src/main/resources/fileContent.txt").withInputStream { stream ->
stream.eachLine { line ->
println line
}
}
}
}

View File

@ -1,71 +1,76 @@
package com.baeldung.file
import spock.lang.Specification
import spock.lang.Ignore
class ReadFileUnitTest extends Specification {
ReadFile readFile
void setup () {
void setup() {
readFile = new ReadFile()
}
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:
def filePath = "src/main/resources/fileContent.txt"
when:
def noOfLines = readFile.readFileLineByLine(filePath)
then:
noOfLines
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:
def filePath = "src/main/resources/fileContent.txt"
when:
def lines = readFile.readFileInList(filePath)
then:
lines
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:
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""")
fileContent.contains(["Line 1 : Hello World!!!", "Line 2 : This is a file content.", "Line 3 : String content"].join("\r\n"))
}
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:
def filePath = "src/main/resources/utf8Content.html"
when:
def encodedContent = readFile.readFileStringWithCharset(filePath)
then:
encodedContent
encodedContent instanceof String
}
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:
def filePath = "src/main/resources/sample.png"
when:
def binaryContent = readFile.readBinaryFile(filePath)
then:
binaryContent
binaryContent instanceof byte[]
binaryContent.length == 329
}
}