利用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(); } } } }
打印结果

本文来自博客园,作者:guoyuxin3,转载请注明原文链接:https://www.cnblogs.com/guoyuxin3/p/15072303.html

浙公网安备 33010602011771号