
- Groovy Tutorial
- Groovy - Home
- Groovy - Overview
- Groovy - Environment
- Groovy - Basic Syntax
- Groovy - Data Types
- Groovy - Variables
- Groovy - Optionals
- Groovy - Numbers
- Groovy - Strings
- Groovy - Ranges
- Groovy - Lists
- Groovy - Maps
- Groovy - Dates & Times
Groovy Operators
- Groovy - Operators
- Groovy - Arithmetic Operators
- Groovy - Assignment Operators
- Groovy - Relational Operators
- Groovy - Logical Operators
- Groovy - Bitwise Operators
- Groovy - Spaceship Operator
- Groovy - in Operator
- Groovy - Elvis Operator
- Groovy - Safe Navigation Operator
- Groovy Operator Precedence & Associativity
Control Statements
- Groovy - Decision Making
- Groovy - If Else Statement
- Groovy - Switch Statement
- Groovy - Loops
- Groovy - For Loop
- Groovy - For-in Loop
- Groovy - While Loop
- Groovy - Do While Loop
- Groovy - Break Statement
- Groovy - Continue Statement
Groovy File Handling
- Groovy - File I/O
- Java - Create a File
- Java - Write to File
- Java - Append to File
- Java - Read Files
- Java - Delete Files
- Java - File Properties
- Java - File Existence and Type
- Java - File Size
- Java - File Permissions
- Java - Directories
- Java - Listing Directories
- Java - Filtering Files/Directories
- Java - Deleting Directories
- Java - Renaming Files/Directories
Groovy Error & Exceptions
- Groovy - Exception Handling
- Groovy - try-catch Block
- Groovy - try-with-resources
- Groovy - Multi-catch Block
- Groovy - Nested try Block
- Groovy - Finally Block
- Groovy - throw Exception
- Groovy - Exception Propagation
- Groovy - Built-in Exceptions
- Groovy - Custom Exception
Groovy Multithreading
- groovy - Multithreading
- groovy - Thread Life Cycle
- groovy - Creating a Thread
- groovy - Starting a Thread
- groovy - Joining Threads
- groovy - Naming Thread
- groovy - Thread Scheduler
- groovy - Thread Pools
- groovy - Main Thread
- groovy - Thread Priority
- groovy - Daemon Threads
- groovy - Shutdown Hook
Groovy Synchronization
- groovy - Synchronization
- groovy - Block Synchronization
- groovy - Static Synchronization
- groovy - Inter-thread Communication
- groovy - Thread Deadlock
- groovy - Interrupting a Thread
- groovy - Thread Control
- groovy - Reentrant Monitor
- Groovy - Methods
- Groovy - Methods
- Groovy - Optional parenthesis
- Groovy - Named Arguments
- Groovy - Closures as Arguments
- Groovy - Method Overloading
- Groovy - Method Scope and Visibility
- Groovy - isCase Method
- Groovy - Implicit Return
- Groovy - Variable Arguments
- Groovy - Regular Expressions
- Groovy - Regular Expressions
- Groovy - Defining Regular Expressions
- Groovy - Matcher Object
- Groovy - Regex Tasks
- Groovy - XML
- Groovy - XML
- Groovy - Parsing XML
- Groovy - Creating XML
- Groovy - Modifying XML
- Groovy - Querying XML
- Groovy - Simplified Notation
- Groovy - Closure based Querying
- Groovy - Closure based Creation
- Groovy - JSON
- Groovy - JSON
- Groovy - Parsing JSON
- Groovy - Creating JSON using JsonOutput
- Groovy - Creating JSON using JsonBuilder
- Groovy - Modifying JSON
- Groovy - Error Handling
- Groovy - Handling JSON Arrays
- Groovy - JSON Array Operations
- Groovy - JSON Objects
- Groovy - JSON Object Operations
- Groovy - Generics
- Groovy - Generics
- Groovy - Declaring Generic Types
- Groovy - Bound Type Parameters
- Groovy - Wild Cards
- Groovy - Miscellaneous
- Groovy - Object Oriented
- Groovy - Closures
- Groovy - Annotations
- Groovy - JMX
- Groovy - DSLS
- Groovy - Database
- Groovy - Builders
- Groovy - Command Line
- Groovy - Unit Testing
- Groovy - Template Engines
- Groovy - Meta Object Programming
- Groovy Useful Resources
- Groovy - Quick Guide
- Groovy - Useful Resources
- Groovy - Discussion
Groovy - Read File
Read a File in Groovy
We can read to a file in Groovy using multiple ways. Following are three most popular ways to create a file in Groovy −
-
Using FileInputStream() constructor
Using File.eachLine method
Using File.text method
Let's take a look at each of the way to read file in Groovy.
Reading a File Using FileInputStream Constructor
FileInputStream is used for reading data from the files. Objects can be created using the keyword new and there are several types of constructors available.
Syntax
Following constructor takes a file name as a string to create an input stream object to read the file −
InputStream f = new FileInputStream("test.txt");
Example - Readng File Using FileInputStream Constructor
Following is the example to demonstrate FileInputStream to read a file from current directory −
Example.groovy
class Example { public static void main(String[] args) { try { byte[] bytes = [65, 66, 67, 68, 69]; OutputStream os = new FileOutputStream("test.txt"); for(int x = 0; x < bytes.length ; x++) { os.write( bytes[x] ); // writes the bytes } os.close(); InputStream is = new FileInputStream("test.txt"); int size = is.available(); for(int i = 0; i < size; i++) { System.out.print((char)is.read()); } is.close(); } catch (IOException e) { System.out.print("Exception"); } } }
The above code would create file test.txt and would write given numbers in binary format. Same would be read using FileInputStream and the output is printed on the stdout screen.
Output
ABCDE
Reading File Content Using File.eachLine() Method
The following example will output all the lines of a text file in Groovy. The method eachLine is in-built in the File class in Groovy for the purpose of ensuring that each line of the text file is read.
Syntax
new File("test.txt").eachLine { line -> println "line : $line"; }
Example: Reading File Content Using File.eachLine() Method
Following is the example to demonstrate File.eachLine to read a file line by line. −
class Example { static void main(String[] args) { try { new File('test.txt').withWriter('utf-8') { writer -> { writer.writeLine 'Line 1'; writer.writeLine 'Line 2'; } } new File('test.txt').eachLine { line -> println "line : $line"; } } catch (IOException e) { System.out.print("Exception"); } } }
The above code would create file test.txt and would write given string in text format. Same would be the output on the stdout screen.
Output
line : Line 1 line : Line 2
Reading File Content Using File.text() Method
The following example will output all the lines of a text file in Groovy. The method text is in-built in the File class in Groovy to read content of the file as a string.
Syntax
File file = new File("test.txt") println file.text
Example: Reading File Content Using File.text Method
Following is the example to demonstrate File.text to read a file content all at once −
class Example { static void main(String[] args) { try { new File('test.txt').withWriter('utf-8') { writer -> { writer.writeLine 'Line 1'; writer.writeLine 'Line 2'; } } File file = new File("test.txt") println file.text } catch (IOException e) { System.out.print("Exception"); } } }
The above code would create file test.txt and would write given string in text format. Same would be the output on the stdout screen.
Output
Line 1 Line 2