Java - 行ごとにテキストファイルを読み込む

Java –テキストファイルを1行ずつ読み取る

この記事では、テキストファイルを1行ずつ読み取る3つの方法について説明します。

  1. Java NIOライブラリ–ファイルを読み取るためのFileChannel。 これは、ファイルを読み取る非ブロッキングモードです。

  2. BufferedReader –これはブロック操作です。つまり、タスクが完了するまで、ファイルに対する他の読み取り/書き込み要求をブロックします。

  3. Apache Commons IO –FileUtils、ファイルを1行ずつ読み取る簡単な方法。 また、ブロッキング操作です。

各例の入力ファイルは、次に示すものと同じです。

src/com/techfou/data.txt

A
B
C
D
1
2
3

1. Java NIOライブラリ

この例は、小さなファイルを読み取る最も簡単な方法を示しています。 これは小さなファイルなので、FileChannelサイズを使用して必要なメモリをByteBufferに直接割り当てます。

NIOFileReadExample.java

package com.techfou;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class NIOFileReadExample {

    public static void main(String[] args) throws IOException {

        RandomAccessFile file = new RandomAccessFile("src/com/example/data.txt", "r");

        FileChannel channel = file.getChannel();

        System.out.println("File size is: " + channel.size());

        ByteBuffer buffer = ByteBuffer.allocate((int) channel.size());

        channel.read(buffer);

        buffer.flip();//Restore buffer to position 0 to read it

        System.out.println("Reading content and printing ... ");

        for (int i = 0; i < channel.size(); i++) {
            System.out.print((char) buffer.get());
        }

        channel.close();
        file.close();

    }

}

コードを実行すると、次の出力が得られます。

File size is: 19
Reading content and printing ...
A
B
C
D
1
2
3

2. BufferedReader

ReadTextFile.java

package com.example;

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

public class ReadTextFile {

    public static void main(String[] args) throws IOException {

        try {

            File f = new File("src/com/example/data.txt");

            BufferedReader b = new BufferedReader(new FileReader(f));

            String readLine = "";

            System.out.println("Reading file using Buffered Reader");

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

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

    }

}

このコードを実行すると、出力は次のようになります。

Reading file using Buffered Reader
A
B
C
D
1
2
3

Note
その他のBufferedReader examples

3. Apache Commons IO

pom.xml

  
    commons-io
    commons-io
    2.5
  

FileIOUtilsExample

package com.example;

import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.util.List;

public class ReadTextFile {

    public static void main(String[] args) throws IOException {

        try {

            File f = new File("src/com/example/data.txt");

            System.out.println("Reading files using Apache IO:");

            List lines = FileUtils.readLines(f, "UTF-8");

            for (String line : lines) {
                System.out.println(line);
            }

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

    }

}

上記のコードを実行すると、出力は次のようになります。

Reading files using Apache IO:
A
B
C
D
1
2
3

Java 8
これを試してください-Java 8 stream to read file line by line