How do I evaluate or execute a script file?

This example demonstrate how to evaluate script that stored on a file. As we know that the eval() method can also accept a Reader object we can then read the script file using FileReader and pass it as the parameter to the eval() method of the ScriptEngine for further evaluation.

package org.kodejava.example.script;

import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
import java.io.File;
import java.io.Reader;
import java.io.FileReader;
import java.io.FileNotFoundException;

public class EvalScriptFile {
    public static void main(String[] args) {
        // Obtaining ECMAScript / JavaScript ScriptEngine.
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("ECMAScript");

        try {
            // Create an instance of File object that point to our
            // scripting file. An create a FileReader to read the
            // file to be passed to the ScriptEngine.eval() method.
            //
            // The file need to be placed in the same folder with
            // our class so it enable to read it. We can define the
            // full path to the file also to make easier for the
            // Reader to find it.
            File script = new File("helloworld.js");
            Reader reader = new FileReader(script);

            engine.eval(reader);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (ScriptException e) {
            e.printStackTrace();
        }
    }
}

How do I use LineNumberReader class to read file?

In this example we use LineNumberReader class to read file contents. What we try to do here is to get the line number of the read data. Instead of introducing another variable; an integer for instance; to keep track the line number we can utilize the LineNumberReader class. This class offers the getLineNumber() method to know the current line of the data that is read.

package org.kodejava.example.io;

import java.io.FileReader;
import java.io.LineNumberReader;

public class LineNumberReaderExample {
    public static void main(String[] args) throws Exception {
        // We'll read a file called student.csv that contains our
        // student information data.
        String filename = Thread.currentThread().getContextClassLoader()
            .getResource("student.csv").getFile();

        // To create the FileReader we can pass in our student data
        // file to the reader. Next we pass the reader into our
        // LineNumberReader class.
        try (FileReader fileReader = new FileReader(filename);
             LineNumberReader lineNumberReader = new LineNumberReader(fileReader)) {
            // If we set the line number of the LineNumberReader here
            // we'll got the line number start from the defined line
            // number + 1

            //lineNumberReader.setLineNumber(400);

            String line;
            while ((line = lineNumberReader.readLine()) != null) {
                // We print out the student data and show what line
                // is currently read by our program.
                System.out.printf("Line Number %s: %s%n",
                    lineNumberReader.getLineNumber(), line);
            }
        }
    }
}

The /resources/student.csv file:

Alice, 7
Bob, 8
Carol, 5
Doe, 6
Earl, 6
Malory, 8

And here is the result of our code snippet above:

Line Number 1: Alice, 7
Line Number 2: Bob, 8
Line Number 3: Carol, 5
Line Number 4: Doe, 6
Line Number 5: Earl, 6
Line Number 6: Malory, 8