0
0

Javaで「ファイルからの入力を行うスキャナを作成する」の動作を確認してみた

Posted at

概要

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.

まとめ

何かの役に立てばと。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0