BAEL-2659 - Reading a file in Groovy - changed order of functions
This commit is contained in:
parent
ac53498b76
commit
f22d1073d1
|
@ -2,28 +2,6 @@ package com.baeldung.file
|
||||||
|
|
||||||
class ReadFile {
|
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
|
* reads file content line by line using withReader and reader.readLine
|
||||||
* @param filePath
|
* @param filePath
|
||||||
|
@ -52,10 +30,28 @@ class ReadFile {
|
||||||
def lines = file.readLines()
|
def lines = file.readLines()
|
||||||
return lines
|
return lines
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
/**
|
||||||
def file = new File("../../src")
|
* reads file content in string using File.text
|
||||||
println file.directorySize
|
* @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
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
|
@ -10,6 +10,28 @@ class ReadFileUnitTest extends Specification {
|
||||||
readFile = new ReadFile()
|
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' () {
|
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"
|
||||||
|
@ -33,26 +55,4 @@ Line 3 : String content""")
|
||||||
noOfLines
|
noOfLines
|
||||||
noOfLines instanceof String
|
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
|
|
||||||
}
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue