BAEL-2659 - Reading a file in Groovy
This commit is contained in:
parent
8ccec9a413
commit
ac53498b76
|
@ -0,0 +1,61 @@
|
||||||
|
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
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
int readFileLineByLine(String filePath) {
|
||||||
|
File file = new File(filePath)
|
||||||
|
def line, noOfLines = 0;
|
||||||
|
file.withReader { reader ->
|
||||||
|
while ((line = reader.readLine())!=null)
|
||||||
|
{
|
||||||
|
println "${line}"
|
||||||
|
noOfLines++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return noOfLines
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* reads file content in list of lines
|
||||||
|
* @param filePath
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
List<String> readFileInList(String filePath) {
|
||||||
|
File file = new File(filePath)
|
||||||
|
def lines = file.readLines()
|
||||||
|
return lines
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
def file = new File("../../src")
|
||||||
|
println file.directorySize
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,3 @@
|
||||||
|
Line 1 : Hello World!!!
|
||||||
|
Line 2 : This is a file content.
|
||||||
|
Line 3 : String content
|
|
@ -0,0 +1,58 @@
|
||||||
|
package com.baeldung.file
|
||||||
|
|
||||||
|
import spock.lang.Specification
|
||||||
|
|
||||||
|
class ReadFileUnitTest extends Specification {
|
||||||
|
|
||||||
|
ReadFile readFile
|
||||||
|
|
||||||
|
void setup () {
|
||||||
|
readFile = new ReadFile()
|
||||||
|
}
|
||||||
|
|
||||||
|
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""")
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
def 'Should return UTF-8 encoded file content in string using ReadFile.readFileStringWithCharset given filePath' () {
|
||||||
|
given:
|
||||||
|
def filePath = "src/main/resources/fileContent.txt"
|
||||||
|
when:
|
||||||
|
def noOfLines = readFile.readFileStringWithCharset(filePath)
|
||||||
|
then:
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue