利用IO(文件字符流)打印代码本身

package excise;

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

public class PrintMyself {
    public static void main(String[] args) {
        FileReader fr = null;
        try {
            fr = new FileReader("src/excise/PrintMyself.java");
            char[] chars = new char[100];
            int redCount=0;
            while ((redCount = fr.read(chars)) != -1){
                System.out.print(new String(chars,0,redCount));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

打印结果

 

posted @ 2021-07-28 19:57  guoyuxin3  阅读(44)  评论(0)    收藏  举报