Reformat and single liner `read*` methods
This commit is contained in:
parent
ab5c09a958
commit
c34a4ac328
|
@ -10,13 +10,14 @@ class ReadFile {
|
||||||
int readFileLineByLine(String filePath) {
|
int readFileLineByLine(String filePath) {
|
||||||
File file = new File(filePath)
|
File file = new File(filePath)
|
||||||
def line, noOfLines = 0;
|
def line, noOfLines = 0;
|
||||||
|
|
||||||
file.withReader { reader ->
|
file.withReader { reader ->
|
||||||
while ((line = reader.readLine())!=null)
|
while ((line = reader.readLine()) != null) {
|
||||||
{
|
|
||||||
println "${line}"
|
println "${line}"
|
||||||
noOfLines++
|
noOfLines++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return noOfLines
|
return noOfLines
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -26,9 +27,7 @@ class ReadFile {
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
List<String> readFileInList(String filePath) {
|
List<String> readFileInList(String filePath) {
|
||||||
File file = new File(filePath)
|
return new File(filePath).readLines()
|
||||||
def lines = file.readLines()
|
|
||||||
return lines
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -37,9 +36,7 @@ class ReadFile {
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
String readFileString(String filePath) {
|
String readFileString(String filePath) {
|
||||||
File file = new File(filePath)
|
return new File(filePath).text
|
||||||
String fileContent = file.text
|
|
||||||
return fileContent
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -48,9 +45,7 @@ class ReadFile {
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
String readFileStringWithCharset(String filePath) {
|
String readFileStringWithCharset(String filePath) {
|
||||||
File file = new File(filePath)
|
return new File(filePath).getText("UTF-8")
|
||||||
String utf8Content = file.getText("UTF-8")
|
|
||||||
return utf8Content
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -59,49 +54,44 @@ class ReadFile {
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
byte[] readBinaryFile(String filePath) {
|
byte[] readBinaryFile(String filePath) {
|
||||||
File file = new File(filePath)
|
return new File(filePath).bytes
|
||||||
byte[] binaryContent = file.bytes
|
|
||||||
return binaryContent
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* More Examples of reading a file
|
* More Examples of reading a file
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
def moreExamples() {
|
def moreExamples() {
|
||||||
|
|
||||||
//with reader with utf-8
|
//with reader with utf-8
|
||||||
new File("src/main/resources/utf8Content.html").withReader('UTF-8') { reader ->
|
new File("src/main/resources/utf8Content.html").withReader('UTF-8') { reader ->
|
||||||
def line
|
def line
|
||||||
while ((line = reader.readLine())!=null) {
|
while ((line = reader.readLine()) != null) {
|
||||||
println "${line}"
|
println "${line}"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//collect api
|
// collect api
|
||||||
def list = new File("src/main/resources/fileContent.txt").collect {it}
|
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[]
|
def array = new File("src/main/resources/fileContent.txt") as String[]
|
||||||
|
|
||||||
//eachline
|
// eachline
|
||||||
new File("src/main/resources/fileContent.txt").eachLine { line ->
|
new File("src/main/resources/fileContent.txt").eachLine { println it }
|
||||||
println line
|
|
||||||
}
|
|
||||||
|
|
||||||
//newInputStream with eachLine
|
//newInputStream with eachLine
|
||||||
def is = new File("src/main/resources/fileContent.txt").newInputStream()
|
|
||||||
is.eachLine {
|
// try-with-resources automatically closes BufferedInputStream resource
|
||||||
println it
|
try (def inputStream = new File("src/main/resources/fileContent.txt").newInputStream()) {
|
||||||
|
inputStream.eachLine { println it }
|
||||||
}
|
}
|
||||||
is.close()
|
|
||||||
|
// withInputStream
|
||||||
//withInputStream
|
|
||||||
new File("src/main/resources/fileContent.txt").withInputStream { stream ->
|
new File("src/main/resources/fileContent.txt").withInputStream { stream ->
|
||||||
stream.eachLine { line ->
|
stream.eachLine { line ->
|
||||||
println line
|
println line
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
|
||||||
|
|
|
@ -1,71 +1,76 @@
|
||||||
package com.baeldung.file
|
package com.baeldung.file
|
||||||
|
|
||||||
import spock.lang.Specification
|
import spock.lang.Specification
|
||||||
import spock.lang.Ignore
|
|
||||||
|
|
||||||
class ReadFileUnitTest extends Specification {
|
class ReadFileUnitTest extends Specification {
|
||||||
|
|
||||||
ReadFile readFile
|
ReadFile readFile
|
||||||
|
|
||||||
void setup () {
|
void setup() {
|
||||||
readFile = new ReadFile()
|
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:
|
given:
|
||||||
def filePath = "src/main/resources/fileContent.txt"
|
def filePath = "src/main/resources/fileContent.txt"
|
||||||
|
|
||||||
when:
|
when:
|
||||||
def noOfLines = readFile.readFileLineByLine(filePath)
|
def noOfLines = readFile.readFileLineByLine(filePath)
|
||||||
|
|
||||||
then:
|
then:
|
||||||
noOfLines
|
noOfLines
|
||||||
noOfLines instanceof Integer
|
noOfLines instanceof Integer
|
||||||
assert noOfLines, 3
|
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"
|
|
||||||
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' () {
|
def 'Should return File Content in list of lines using ReadFile.readFileInList given filePath'() {
|
||||||
given:
|
given:
|
||||||
def filePath = "src/main/resources/utf8Content.html"
|
def filePath = "src/main/resources/fileContent.txt"
|
||||||
|
|
||||||
when:
|
when:
|
||||||
def encodedContent = readFile.readFileStringWithCharset(filePath)
|
def lines = readFile.readFileInList(filePath)
|
||||||
|
|
||||||
then:
|
then:
|
||||||
encodedContent
|
lines
|
||||||
encodedContent instanceof String
|
lines instanceof List<String>
|
||||||
|
lines.size() == 3
|
||||||
}
|
}
|
||||||
|
|
||||||
def 'Should return binary file content in byte array using ReadFile.readBinaryFile given filePath' () {
|
def 'Should return file content in string using ReadFile.readFileString given filePath'() {
|
||||||
given:
|
given:
|
||||||
def filePath = "src/main/resources/sample.png"
|
def filePath = "src/main/resources/fileContent.txt"
|
||||||
|
|
||||||
when:
|
when:
|
||||||
def binaryContent = readFile.readBinaryFile(filePath)
|
def fileContent = readFile.readFileString(filePath)
|
||||||
|
|
||||||
then:
|
then:
|
||||||
binaryContent
|
fileContent
|
||||||
binaryContent instanceof byte[]
|
fileContent instanceof String
|
||||||
binaryContent.length == 329
|
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'() {
|
||||||
|
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'() {
|
||||||
|
given:
|
||||||
|
def filePath = "src/main/resources/sample.png"
|
||||||
|
|
||||||
|
when:
|
||||||
|
def binaryContent = readFile.readBinaryFile(filePath)
|
||||||
|
|
||||||
|
then:
|
||||||
|
binaryContent
|
||||||
|
binaryContent instanceof byte[]
|
||||||
|
binaryContent.length == 329
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue