Javaでは、 `java.io.BufferedReader`を使ってファイルからコンテンツを読み込むことができます。

1. Classic BufferedReader

ファイルからコンテンツを読み込む古典的な

BufferedReader

E:\\ test \\ filename.txt

This is the content to write into file
This is the content to write into file

ReadFileExample1.java

package com.mkyong;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class ReadFileExample1 {

    private static final String FILENAME = "E:\\test\\filename.txt";

    public static void main(String[]args) {

        BufferedReader br = null;
        FileReader fr = null;

        try {

           //br = new BufferedReader(new FileReader(FILENAME));
            fr = new FileReader(FILENAME);
            br = new BufferedReader(fr);

            String sCurrentLine;

            while ((sCurrentLine = br.readLine()) != null) {
                System.out.println(sCurrentLine);
            }

        } catch (IOException e) {

            e.printStackTrace();

        } finally {

            try {

                if (br != null)
                    br.close();

                if (fr != null)
                    fr.close();

            } catch (IOException ex) {

                ex.printStackTrace();

            }

        }

    }

}

出力:

This is the content to write into file
This is the content to write into file

2. JDK7の例

`try-with-resources`の例でファイルリーダーを自動的に閉じます。

ReadFileExample2.java

package com.mkyong;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class ReadFileExample2 {

    private static final String FILENAME = "E:\\test\\filename.txt";

    public static void main(String[]args) {

        try (BufferedReader br = new BufferedReader(new FileReader(FILENAME))) {

            String sCurrentLine;

            while ((sCurrentLine = br.readLine()) != null) {
                System.out.println(sCurrentLine);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

参考文献

try-with-resourcesステートメント]。 link://java/how-to-append-content-to-file-in-java/[追加する方法

Javaでファイルするコンテンツ]。

https://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html

[BufferedReader

JavaDoc]。 link://java/how-to-file-in-java-bufferedwriter-example/[どのようにして

Javaでファイルに書き込む – BufferedWriter]