Groovy Quiz 24 | Writing files


Reference video | Groovy Playground


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

1.

Guess the output

File myFile = new File("file1.txt")
myFile.write("This is Line 1")
println myFile.text
 
 
 

2.

// << left shift operator

Guess the output

File myFile = new File("file1.txt")
myFile << "This is Line 1"
 
 
 

3.

Guess the output

File myFile = new File("file1.txt")
myFile << "This is Line 1"
myFile.text = "This is Line 2"
println myFile.text
 
 
 

4.

// using withWriter

Guess the output

File myFile = new File("file1.txt")
myFile.write("This is line 1")

myFile.withWriter { writer ->
    writer.writeLine("This is Line 2")
}
println myFile.text
 
 
 

5.

Guess the output

File myFile = new File("file1.txt")
myFile.write("This is Line 1")
myFile.appent("\nThis is Line 2")
println myFile.text
 
 
 

6.

Guess the output

File myFile = new File("file1.txt")
myFile.write("This is Line 1")

println myFile.length()
println myFile.isFile()
println myFile.isDirectory()
println myFile.isHidden()
 
 

7.

Guess the output

File myFile = new File("file1.txt")
myFile.write("This is Line 1")

File newFile = new File("file2.txt")

newFile << myFile.text
 
 
 
 

8.

Guess the output

File myFile = new File("file1.txt")
myFile.write("This is Line 1")
myFile.bytes = []
 
 
 

9.

// Renaming file

Guess the output

File myFile = new File("file1.txt")
myFile.write("This is Line 1")
myFile.renameTo(new File("newfile.txt"))
 
 

5