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 A file will be created named file1.txt and the text will be written in the file. The text will also get printed on the console A blank file will get created exception 2. // << left shift operator Guess the output File myFile = new File("file1.txt") myFile << "This is Line 1" File with name file1.txt will get created and data will be written in the file An empty file will get created Exception 3. Guess the output File myFile = new File("file1.txt") myFile << "This is Line 1" myFile.text = "This is Line 2" println myFile.text This is Line 1 This is Line 2 This is Line 1 This is Line 2 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 This is Line 1 This is Line 2 This is Line 2 This is Line 1 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 This is Line 1 This is Line 2 This is Line 2 This is Line 1 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() //size of the file in bytes true false false exception 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 No change A new file named file2.txt will get created and data will be copied from file1.txt exception A new blank file will get created 8. Guess the output File myFile = new File("file1.txt") myFile.write("This is Line 1") myFile.bytes = [] Will empty the file file1.txt no change exception 9. // Renaming file Guess the output File myFile = new File("file1.txt") myFile.write("This is Line 1") myFile.renameTo(new File("newfile.txt")) file1.txt will get renamed to newfile.txt A new file named newfile.txt will get created Loading … 5