Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

Java 8 read lines from a text file

farenda 2015-05-21 0

Problem:

How to read lines from a text file in Java 8?

Solution:

Using methods from Files and Paths classes available in Java 8.

package com.farenda.solved;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class JavaSolved {

    public static void main(String[] args) throws IOException {
        if (args.length == 0) {
            System.out.println("Run with a file as a parameter");
        } else {
            System.out.println("Reading from: " + args[0] + " using Java 8");
            // By default readAllLines() uses UTF-8 to load text
            for (String line : Files.readAllLines(Paths.get(args[0]))) {
                System.out.println(line);
            }
        }
    }
}

Compile the code with:

$> javac src/com/farenda/solved/*.java -d out

Result:

Running without arguments prints help:

$> java -cp out com.farenda.solved.JavaSolved
Run with a file as a parameter

Pass a file as an argument:

$> java -cp out com.farenda.solved.JavaSolved src/com/farenda/solved/JavaSolved.java
Reading from: src/com/farenda/solved/JavaSolved.java using Java 8
package com.farenda.solved;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class JavaSolved {

    public static void main(String[] args) throws IOException {
        if (args.length == 0) {
            System.out.println("Run with a file as a parameter");
        } else {
            System.out.println("Reading from: " + args[0] + " using Java 8");
            // By default readAllLines() uses UTF-8 to load text
            for (String line : Files.readAllLines(Paths.get(args[0]))) {
                System.out.println(line);
            }
        }
    }
}
Share with the World!
Categories Java Tags java8-files
Previous: Java “Hello world!”
Next: Java 8 read file into a String

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2022

sponsored