BAEL-2659 - Reading a file in Groovy - changed order of functions

This commit is contained in:
Anshul Bansal 2019-01-27 22:47:27 +02:00
parent ac53498b76
commit f22d1073d1
2 changed files with 44 additions and 48 deletions

View File

@ -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
}
}

View File

@ -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<String>
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<String>
assert lines.size(), 3
}
}