Groovy, a powerful language built on the Java Virtual Machine (JVM), makes file handling and input/output (I/O) operations seamless. This quiz will test your knowledge of Groovy’s file handling features, including reading and writing files, traversing directories, working with streams, and processing data in JSON and XML formats.
new File("file.txt").read()new File("file.txt").textFile.read("file.txt")readFile("file.txt")new File("file.txt").write("Hello World")File.write("file.txt", "Hello World")writeToFile("file.txt", "Hello World")new File("file.txt").append("Hello World")getText() method do in Groovy?
new File("file.txt").createNewFile()File.create("file.txt")createFile("file.txt")new File("file.txt").create()new File("file.txt").append("Hello World")appendToFile("file.txt", "Hello World")File.append("file.txt", "Hello World")new File("file.txt").write("Hello World", append=true)new File("dir").listFiles()listDirectory("dir")new File("dir").files()getFiles("dir")new File("dir").traverse()File.traverse("dir")new File("dir").recursiveFiles()traverseFiles("dir")eachFile method do in Groovy?
eachFileRecursefindFileseachFileMatcheachFileConditionnew File("file.txt").delete()File.remove("file.txt")deleteFile("file.txt")new File("file.txt").remove()new File("file.txt").inputStream()new File("file.txt").getInputStream()File.readStream("file.txt")new File("file.txt").stream()new File("file.txt").outputStream()File.writeStream("file.txt")new File("file.txt").getOutputStream()File.writeToStream("file.txt")new File("file.txt").newReader()new File("file.txt").inputStreamReader()new File("file.txt").newBufferedReader()new File("file.txt").streamReader()new File("source.txt").copyTo("destination.txt")File.copy("source.txt", "destination.txt")new File("source.txt").copyWithStream("destination.txt")File.copyStream("source.txt", "destination.txt")stream.close()new File("file.txt").closeStream()new File("file.txt").flush()outputStream.close()new JsonSlurper().parseText(jsonString)JsonParser.parse(jsonString)parseJson(jsonString)new JsonParser().fromJson(jsonString)JsonGeneratorJsonSlurperJsonOutputJsonParserJsonOutput.toJson(jsonObject, new File("file.json"))new File("file.json").write(JsonOutput.toJson(jsonObject))JsonOutput.writeToFile(jsonObject, "file.json")File.writeJson(jsonObject, "file.json")new XmlParser().parse(new File("file.xml"))XmlParser.parse("file.xml")new File("file.xml").xml()new XmlReader("file.xml").parse()XmlOutputXmlBuilderXmlGeneratorXmlWriterXmlParser.toString()new XmlNodePrinter().print(xmlObject)XmlOutput.toXmlString(xmlObject)xmlObject.toString()XmlOutput.toXml(object)new XmlBuilder().build(object)new XmlParser().toXml(object)object.toXml()new JsonSlurper().parse(new File("file.json"))JsonSlurper.parseJson(new File("file.json"))JsonParser.parseFile("file.json")parseJsonFile("file.json")xml.appendNode(newNode)xml.addChild(newNode)xml.addNode(newNode)xml.createNode(newNode)catch(JsonParseException e)try { ... } catch (IOException e)catch (JsonParseException | IOException e)try { ... } catch (Exception e)xmlNode.toMap()xmlNode.toMapRepresentation()xmlNode.convertToMap()xmlNode.toMapOfNodes()JsonOutput.prettyPrint()JsonFormatter.pretty()JsonPrinter.print()JsonFormatter.prettyPrint()new XmlParser() do in Groovy?
xmlNode.text()xmlNode.value()xmlNode.getText()xmlNode.getValue()JsonBuilderJsonParserJsonOutputJsonGenerator| Qno | Answer |
|---|---|
| 1 | b) new File("file.txt").text |
| 2 | a) new File("file.txt").write("Hello World") |
| 3 | b) Reads the contents of a file as text |
| 4 | a) new File("file.txt").createNewFile() |
| 5 | a) new File("file.txt").append("Hello World") |
| 6 | a) new File("dir").listFiles() |
| 7 | a) new File("dir").traverse() |
| 8 | b) Loops through files in a directory |
| 9 | c) eachFileMatch |
| 10 | a) new File("file.txt").delete() |
| 11 | a) new File("file.txt").inputStream() |
| 12 | a) new File("file.txt").outputStream() |
| 13 | c) new File("file.txt").newBufferedReader() |
| 14 | a) new File("source.txt").copyTo("destination.txt") |
| 15 | a) stream.close() |
| 16 | a) new JsonSlurper().parseText(jsonString) |
| 17 | c) JsonOutput |
| 18 | b) new File("file.json").write(JsonOutput.toJson(jsonObject)) |
| 19 | a) new XmlParser().parse(new File("file.xml")) |
| 20 | b) XmlBuilder |
| 21 | b) new XmlNodePrinter().print(xmlObject) |
| 22 | a) XmlOutput.toXml(object) |
| 23 | a) new JsonSlurper().parse(new File("file.json")) |
| 24 | a) xml.appendNode(newNode) |
| 25 | c) `catch (JsonParseException |
| 26 | a) xmlNode.toMap() |
| 27 | a) JsonOutput.prettyPrint() |
| 28 | a) It parses the XML string into an XML document object. |
| 29 | a) xmlNode.text() |
| 30 | a) JsonBuilder |