集册 Java实例教程 逐字阅读文本文件

逐字阅读文本文件

欢马劈雪     最近更新时间:2020-01-02 10:19:05

487
逐字阅读文本文件

import java.io.*;


public class InputFileTest {
/* from 
时 代 J a v a - nowjava.com*/

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

        InputFileDeclared file = new InputFileDeclared("testing");

        System.out.println(file.getWord());

        System.out.println(file.getWord());

        System.out.println(file.getWord());

    }

}

class InputFileDeclared {


    private FileReader in;


    public InputFileDeclared(String filename) throws FileNotFoundException {

        in = new FileReader(filename);

    }/*来 自 时 代 J a v a - N o w J a v a . c o m*/


    public String getWord() throws IOException {

        int c;

        StringBuffer buf = new StringBuffer();


        do {

            c = in.read();

            if (Character.isWhitespace((char) c))

                return buf.toString();

            else

                buf.append((char) c);

        } while (c != -1);


        return buf.toString();

    }

}


展开阅读全文