java输出自身源代码

mac2022-06-30  40

  如何通过运行程序输出程序源码?

  下面是JAVA实现

public class Quine { public static void main(String[] args) { char q = 34; // Quotation mark character String[] l = { // Array of source code "public class Quine", "{", " public static void main(String[] args)", " {", " char q = 34; // Quotation mark character", " String[] l = { // Array of source code", " ", " };", " for(int i = 0; i < 6; i++) // Print opening code", " System.out.println(l[i]);", " for(int i = 0; i < l.length; i++) // Print string array", " System.out.println(l[6] + q + l[i] + q + ',');", " for(int i = 7; i < l.length; i++) // Print this code", " System.out.println(l[i]);", " }", "}", }; for(int i = 0; i < 6; i++) // Print opening code System.out.println(l[i]); for(int i = 0; i < l.length; i++) // Print string array System.out.println(l[6] + q + l[i] + q + ','); for(int i = 7; i < l.length; i++) // Print this code System.out.println(l[i]); } }

   运行结果

  主要代码在String[] l,通过他输出源码,这个数组中的String[] l的内容是一行空格,这也是他和真正源码的区别,也是一个比较关键的地方。

转载于:https://www.cnblogs.com/liunianfeiyu/p/10159763.html

相关资源:Java的输入输出流实现文件的复制
最新回复(0)