概要
Javaで「ファイルからの入力を行うスキャナを作成する」の動作を確認してみました。以下のページを参考にしました。
実装
以下のファイルを作成しました。
JSample4_3.java
import java.util.Scanner;
import java.io.FileNotFoundException;
import java.io.File;
class JSample4_3{
public static void main(String[] args){
try{
File file = new File("sample2.txt");
Scanner scanner = new Scanner(file, "UTF-8");
while (scanner.hasNextLine()){
String dat = scanner.nextLine();
System.out.println(dat);
}
}catch(FileNotFoundException e){
System.out.println(e);
}
}
}
sample2.txt
Hello.
It's nice weather today, is not it.
Goodbye. See you again.
以下のコマンドを実行しました。
$ javac JSample4_3.java
$ java JSample4_3
Hello.
It's nice weather today, is not it.
Goodbye. See you again.
まとめ
何かの役に立てばと。