File tree Expand file tree Collapse file tree
main/groovy/com/baeldung/file
test/groovy/com/baeldung/file Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -2,28 +2,6 @@ package com.baeldung.file
22
33class ReadFile {
44
5- /**
6- * reads file content in string using File.text
7- * @param filePath
8- * @return
9- */
10- String readFileString (String filePath ) {
11- File file = new File (filePath)
12- String fileContent = file. text
13- return fileContent
14- }
15-
16- /**
17- * reads file content in string with encoding using File.getText
18- * @param filePath
19- * @return
20- */
21- String readFileStringWithCharset (String filePath ) {
22- File file = new File (filePath)
23- String utf8Content = file. getText(" UTF-8" )
24- return utf8Content
25- }
26-
275 /**
286 * reads file content line by line using withReader and reader.readLine
297 * @param filePath
@@ -52,10 +30,28 @@ class ReadFile {
5230 def lines = file. readLines()
5331 return lines
5432 }
55-
56- public static void main (String [] args ) {
57- def file = new File (" ../../src" )
58- println file. directorySize
33+
34+ /**
35+ * reads file content in string using File.text
36+ * @param filePath
37+ * @return
38+ */
39+ String readFileString (String filePath ) {
40+ File file = new File (filePath)
41+ String fileContent = file. text
42+ return fileContent
43+ }
44+
45+ /**
46+ * reads file content in string with encoding using File.getText
47+ * @param filePath
48+ * @return
49+ */
50+ String readFileStringWithCharset (String filePath ) {
51+ File file = new File (filePath)
52+ String utf8Content = file. getText(" UTF-8" )
53+ return utf8Content
5954 }
6055
56+
6157}
Original file line number Diff line number Diff line change @@ -10,6 +10,28 @@ class ReadFileUnitTest extends Specification {
1010 readFile = new ReadFile ()
1111 }
1212
13+ def ' Should return number of lines in File using ReadFile.readFileLineByLine given filePath' () {
14+ given :
15+ def filePath = " src/main/resources/fileContent.txt"
16+ when :
17+ def noOfLines = readFile. readFileLineByLine(filePath)
18+ then :
19+ noOfLines
20+ noOfLines instanceof Integer
21+ assert noOfLines, 3
22+ }
23+
24+ def ' Should return File Content in list of lines using ReadFile.readFileInList given filePath' () {
25+ given :
26+ def filePath = " src/main/resources/fileContent.txt"
27+ when :
28+ def lines = readFile. readFileInList(filePath)
29+ then :
30+ lines
31+ lines instanceof List<String >
32+ assert lines. size(), 3
33+ }
34+
1335 def ' Should return file content in string using ReadFile.readFileString given filePath' () {
1436 given :
1537 def filePath = " src/main/resources/fileContent.txt"
@@ -33,26 +55,4 @@ Line 3 : String content""")
3355 noOfLines
3456 noOfLines instanceof String
3557 }
36-
37- def ' Should return number of lines in File using ReadFile.readFileLineByLine given filePath' () {
38- given :
39- def filePath = " src/main/resources/fileContent.txt"
40- when :
41- def noOfLines = readFile. readFileLineByLine(filePath)
42- then :
43- noOfLines
44- noOfLines instanceof Integer
45- assert noOfLines, 3
46- }
47-
48- def ' Should return File Content in list of lines using ReadFile.readFileInList given filePath' () {
49- given :
50- def filePath = " src/main/resources/fileContent.txt"
51- when :
52- def lines = readFile. readFileInList(filePath)
53- then :
54- lines
55- lines instanceof List<String >
56- assert lines. size(), 3
57- }
5858}
You can’t perform that action at this time.
0 commit comments