Easily read text files in Java (Java 11 & Java 7)

Overview

--Use Files.readString and Path.of in Java 11 environment --Use Files.readAllLines, Files.readAllBytes and Paths.get in Java 7 environment

Sample code Cat.java

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

public class Cat {

    public static void main(String[] args) throws IOException {
        String path = args[0];
        System.out.print(readString11(path));
        System.out.print(readString7(path));
        // String.join is Java 8 or later
        System.out.println(String.join(System.lineSeparator(), readLines7(path)));
    }

    /**
     *Read a text file. Methods available from Java 11.
     * @param path path of the file to read
     * @contents of return file
     * @throws IOException
     * @throws OutOfMemoryError
     * @throws RuntimeException
     */
    public static String readString11(String path) throws IOException {
        return Files.readString(Path.of(path), Charset.forName("UTF-8"));
    }

    /**
     *Read a text file. Methods available from Java 7.
     * @param path path of the file to read
     * @contents of return file
     * @throws IOException
     * @throws RuntimeException
     */
    public static String readString7(String path) throws IOException {
        return new String(Files.readAllBytes(Paths.get(path)), Charset.forName("UTF-8"));
    }

    /**
     *Read a text file. Methods available from Java 7.
     * @param path path of the file to read
     * @contents of return file
     * @throws IOException
     * @throws RuntimeException
     */
    public static List<String> readLines7(String path) throws IOException {
        return Files.readAllLines(Paths.get(path), Charset.forName("UTF-8"));
    }
}

Sample execution result

compile.

$ javac Cat.java

Prepare a text file.

$ cat sample.txt 
sample
sample
SANNPURU

Run the sample code.

$ java Cat sample.txt 
sample
sample
SANNPURU
sample
sample
SANNPURU
sample
sample
SANNPURU

Operation check environment

$ java -version
java version "11.0.1" 2018-10-16 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.1+13-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.1+13-LTS, mixed mode)

$ uname -mrsv
Darwin 18.2.0 Darwin Kernel Version 18.2.0: Fri Oct  5 19:41:49 PDT 2018; root:xnu-4903.221.2~2/RELEASE_X86_64 x86_64

$ sw_vers
ProductName:    Mac OS X
ProductVersion: 10.14.1
BuildVersion:   18B75

Reference material

--English reference

--Japanese reference

Recommended Posts

Easily read text files in Java (Java 11 & Java 7)
Read binary files in Java 1
Read binary files in Java 2
Read JSON in Java
Read standard input in Java
Read Java properties file in C #
Read CSV in Java (Super CSV Annotation)
Text file placed in resources in Java cannot be read when jarted
Convert SVG files to PNG files in Java
Read Felica using RC-S380 (PaSoRi) in Java
Play RAW, WAV, MP3 files in Java
Read xlsx file in Java with Selenium
Reading and writing gzip files in Java
Partization in Java
Changes in Java 11
Java introductory text
Rock-paper-scissors in Java
Pi in Java
FizzBuzz in Java
Read a string in a PDF file with Java
Text extraction in Java from PDF with pdfbox-2.0.8
Display text as ASCII art in Java (jfiglet)
Add, read, and delete Excel comments in Java
[Java] Read the file in src / main / resources
Change paragraph text color in Java Word documents
Import files of the same hierarchy in Java
[java] sort in list
Interpreter implementation in Java
Rock-paper-scissors app in Java
Constraint programming in Java
Put java8 in centos7
NVL-ish guy in Java
Read the packet capture obtained by tcpdump in Java
Combine arrays in Java
"Hello World" in Java
Read Java Property file
Callable Interface in Java
Java extracts text content of SmartArt shapes in PowerPoint
How to input / output IBM mainframe files in Java?
[Java] Integer information of characters in a text file acquired by the read () method
Comments in Java source
Azure functions in java
Format XML in Java
Simple htmlspecialchars in Java
Boyer-Moore implementation in Java
Read Java HashMap source
Hello World in Java
Use OpenCV in Java
webApi memorandum in java
Android-Upload image files to Azure Blob Storage in Java
Type determination in Java
Ping commands in Java
Various threads in java
Heapsort implementation (in java)
Zabbix API in Java
ASCII art in Java
Compare Lists in Java
POST JSON in Java
Express failure in Java
Create JSON in Java
Date manipulation in Java 8