Java – 行ごとにテキストファイルを読み込む
この記事では、テキストファイルを1行ずつ読み込む3つの方法について説明します。
-
Java NIOライブラリ – ファイルを読むための
FileChannel
. それは
ファイルを読み取るノンブロッキングモード
。
BufferedReader
– ブロッキング操作です。つまり、
他の読み取り/書き込み要求がタスクを完了するまでファイルに書き込みます。
-
Apache Commons IO –
FileUtils
、ファイルを読むためのより簡単な方法
ライン。また、これはブロッキング操作です。
各例の入力ファイルは、以下に示すものと同じです。
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/mkyong/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.mkyong;
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/mkyong/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
3. Apache Commons IO
pom.xml
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
FileIOUtilsExample
package com.mkyong;
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/mkyong/data.txt");
System.out.println("Reading files using Apache IO:");
List<String> 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 ** +これを試してください –
ファイルを1行ずつ読み込むJava 8ストリーム
参考文献
BufferedReaderについて]。
https://docs.oracle.com/javase/7/docs/api/java/io/FileReader.html%E2%80%9D%20target=
[more
FileReaderについて]。リンク://java/how-to-read-file-from-java-bufferedreader-example/[BufferedReader
例]。リンク://java8/java-8-stream-read-a-file-line-by-line/[Java 8ストリームから
ファイルを行単位で読む]。
http://tutorials.jenkov.com/java-nio/file-channel.html
[詳細
FileChannel関数]。
https://coderanch.com/t/611255/java/ByteBuffer-methods-flip-rewind
[Difference
ByteBuffer flip()メソッドとrewind()メソッドの間]。
https://docs.oracle.com/javase/7/docs/api/java/nio/ByteBuffer.html
[ByteBuffer
javadocs]。
https://docs.oracle.com/javase/7/docs/api/java/nio/channels/FileChannel.html
[FileChannel
Javadocs]。
https://commons.apache.org/proper/commons-io/description.html
[Apache
Commons IOユーザーガイド]