Groovy Quiz 23 | Reading files


Reference video | Groovy Playground


1 Point for every correct answer | Let’s Get Started

1.

//reading entire content of file as String

The below statement will get the file and print its contents

File myFile = new File("location of the file")
println myFile.txt
 
 
 

2.

//collecting file contents (lines) in a list

Lets say there is a file called file1.txt and has the following 3 lines

Line 1
Line 2
Line 3

What will be the output of below code

File myFile = new File("location of the file1.txt")
def list = myFile.collect { it }
println "list : $list"
 
 
 

3.

//store file contents in an array

Lets say there is a file called file1.txt and has the following 3 lines

Line 1
Line 2
Line 3

Guess the output

File myFile = new File("location of the file1.txt")
def array = myFile as String[]
println "array : $array"
 
 
 

4.

//read file into a list of Strings

Lets say there is a file called file1.txt and has the following 3 lines

Line 1
Line 2
Line 3

Guess the output

File myFile = new File("location of file1.txt")
def lines = myFile.readLines()
println "lines : $lines"
 
 
 

5.

//read file line by line

Let’s say there is a file called file1.txt and has the following 3 lines

Line 1
Line 2
Line 3

Guess the output

File myFile = new File("location of file1.txt")
myFile.eachLine { line ->
    println "line : $line"
}
 
 
 
 

6.

//read file line by line with Line no

Let’s say there is a file called file1.txt and has the following 3 lines

Line 1
Line 2
Line 3

Guess the output

File myFile = new File("location of file1.txt")
myFile.eachLine { line , lineNo ->
    println "$lineNo : $line"
}
 
 

7.

//add lines to a list with condition

Let’s say there is a file called file1.txt and has the following 3 lines

Line 1
Line 2
Line 3

Guess the output

File myFile = new File("location of file1.txt")

def lineNoRange = 2..4
def lineList = []
myFile.eachLine { line , lineNo ->
    if(lineNoRange.contains(lineNo){
        lineList.add(line)
    }
}
println "lineList : $lineList"
 
 
 

8.

//read with reader

Let’s say there is a file called file1.txt and has the following 3 lines

Line 1
Line 2
Line 3

Guess the output

File myFile = new File("location of file1.txt")

def line
myFile.withReader { reader ->
    while((line = reader.readLine()) != null){
        println line
    }
}
 
 
 

9.

//read with new reader function

Let’s say there is a file called file1.txt and has the following 3 lines

Line 1
Line 2
Line 3

Guess the output

File myFile = new File("location of file1.txt")

def outputFile = file2.txt
def reader = myFile.newReader()
new File(outputFile).append(reader)
reader.close()
 
 
 
 

10.

What is the use of new reader function

 
 
 

11.

//when working with binary files OR need to read content as Bytes

Let’s say there is a file called file1.txt and has the following 3 lines

Line 1
Line 2
Line 3

Guess the output

File myFile = new File("location of file1.txt")

byte[] contents = myFile.bytes
println contents
 
 

12.

//get file size in bytes

Let’s say there is a file called file1.txt and has the following 3 lines

Line 1
Line 2
Line 3

Guess the output

File myFile = new File("location of file1.txt")

print myFile.length()
 
 

13.

//check the is the location is a file or directory

Let’s say there is a file called file1.txt and has the following 3 lines

Line 1
Line 2
Line 3

Guess the output

File myFile = new File("location of file1.txt")

println myFile.isFile()
println myFile.isDirectory()
 
 
 
 

14.

//get the list of files from a directory

Guess the output

new File("location of a directory(folder)").eachFile { 
    
    files -> println files.getAbsolutePath()

}
 
 
 
 

15.

//recursively display all files in a dir and its sub-directory – eachFileRecurse function

Guess the output

new File("location of a directory(folder)").eachFileRecurse { 
    
    file -> println file.getAbsolutePath()

}
 
 

16.

//copy file data – left shift operator <<

Let’s say there is a file called file1.txt and has the following 3 lines

Line 1
Line 2
Line 3

Guess the output

File myFile = new File("file1.txt")

def newFile = new File("file2.txt")
newFile << myFile.text
 
 
 

17.

//delete file

Let’s say there is a file called file1.txt and has the following 3 lines

Line 1
Line 2
Line 3

Will this code delete the file file1.txt

File myFile = new File("file1.txt")
myFile.delete()
 
 

5