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 file into a String

farenda 2015-05-22 0

Problem:

How to read file into a String 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.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;

public class JavaSolved {

    // It's good to move such code to reusable methods
    private static String loadFile(String filename) throws IOException {
        // need to pass file encoding
        return new String(Files.readAllBytes(Paths.get(filename)),
                          StandardCharsets.UTF_8);
    }

    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");
            System.out.println(loadFile(args[0]));
        }
    }
}

Result:

  1. Create sample file books.txt with the content:
    "Thinking in Java"
    "Head First Java"
    "Design Patterns: Elements of Reusable Object-Oriented Software"
    "Applying UML and Patterns"
    "Agile Software Development, Principles, Patterns, and Practices"
    
  2. Compile the code:
    $> javac src/com/farenda/solved/*.java -d out
    
  3. Run with the file as parameter
    $> java -cp out com.farenda.solved.JavaSolved books.txt
    
    Reading from: books.txt using Java 8
    "Thinking in Java"
    "Head First Java"
    "Design Patterns: Elements of Reusable Object-Oriented Software"
    "Applying UML and Patterns"
    "Agile Software Development, Principles, Patterns, and Practices"
    
Share with the World!
Categories Java Tags java8-files
Previous: Java 8 read lines from a text file
Next: Java primitive types default values

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