トップページ >  Java >  ファイルを1行読み込む
初版2008/05/08: 最終更新日2008/05/08
  ファイルを1行読み込む
目次
readLine()メソッド
readLine()メソッド
java.io.BufferedReaderクラスのreadLine()メソッドでファイルの内容を1行読み込むことが可能です。

以下、例です。

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class Test{
    public static void main(String[] args) {
        FileInputStream     fis            = null;
        BufferedReader        readFile    = null;
        
        try {
             fis            = new FileInputStream("c:\\test1\\test2\\a.txt");
            readFile    = new BufferedReader(new InputStreamReader(fis, "Shift_JIS"));
        } 
        catch(Exception e) {
            e.printStackTrace();
        }
        
        String line;
        try {
            while((line = readFile.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                fis.close();
                readFile.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
c:\test1\test2\a.txtファイルを読み込み、readLine()メソッドで1行ずつ読み込んで標準出力しています。


Information
リンクについて
個人情報保護方針
Yahoo!ブックマークに登録

社長&社員ブログ
やる気はあるがお金がない㈱コンフレッジブログ

slot大好きな㈱コンフレッジ社員のブログ
広告

サイト内検索
当サイト内を検索できます↓


PV