fileReader-注意循环返回的值-1-2023-3-3

package JavaSE.Lesson18;




import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class ReaderDemo {
    public static void main(String[] args) {
        String str = "e:\\Demo02.txt";
        FileReader fr= null;
        char[] buf = new char[8];
        int dataLen;
        try {
            fr = new FileReader(str);
            //这个返回的是数值的值
            while ((dataLen=fr.read(buf))!=-1){
                System.out.print(new String(buf,0,dataLen));
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fr!=null){
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }
}
posted @ 2023-03-03 21:56  Rui2022  阅读(8)  评论(0编辑  收藏  举报