日期:2014-05-20  浏览次数:20742 次

游戏一下:写一个程序,打印程序自己的内容。
:)

------解决方案--------------------
//: c11:TestEOF.java
// Testing for the end of file
// while reading a byte at a time.
// From 'Thinking in Java, 3rd ed. ' (c) Bruce Eckel 2002
// www.BruceEckel.com. See copyright notice in CopyRight.txt.
package c11;
import com.bruceeckel.simpletest.*;
import java.io.*;

public class TestEOF {
static Test monitor = new Test();
// Throw exceptions to console:
public static void main(String[] args)
throws IOException {
DataInputStream in =
new DataInputStream(
new BufferedInputStream(
new FileInputStream( "F:\\javaTestPrj\\src\\c11\\TestEOF.java ")));
while(in.available() != 0) {
System.out.print((char)in.readByte());
}
monitor.expect( "F:\\javaTestPrj\\src\\c11\\TestEOF.out ");
}
} ///:~
------解决方案--------------------

------解决方案--------------------
都编译成class了,哪来的程序自己的内容?除非写个反编译的程序
------解决方案--------------------
import java.io.*;

public class Print
{
public static void main(String[] args)throws IOException{
BufferedReader br=new BufferedReader(new FileReader( "1.txt "));
String s=null;
PrintWriter pw=new PrintWriter(System.out);
while((s=br.readLine())!=null){
pw.println(s);
}
pw.close();
}
}
------解决方案--------------------
无聊
------解决方案--------------------
我感觉此问题不能实现,原因如下:
1.因为所写java代码是随机的,故此程序之内必须包含将所写代码保存在固定位置之内才可以调用打印
2.又因楼主不让此java文件存在固定的路径下以及采用固定的类明,故也不能实现文件的读入写出,小弟愚笨,实在想不出有什么办法实现
等高人出现,帮你顶!
------解决方案--------------------
死循环??打印语句也是你程序的内容,你怎么把这个打印语句打出来呢?
------解决方案--------------------
楼主想干啥?自省?
------解决方案--------------------
但是这个能运行吗?

' 在 Java 里可以用这个吗?
------解决方案--------------------
调用反编译工具,或者其他办法,反编译本身,然后用io写出,不过格式肯定不一致了
------解决方案--------------------
新手,解决不了,不过楼主的提问很有创意..
------解决方案--------------------
这个老早的了,现在的程序员忙着赚钱,谁有空研究这个破玩意
------解决方案--------------------
似乎明白了意思,刚刚还在看API里的string啊,呵呵
------解决方案--------------------
Java code
class   S{
public   static   void   main(String[]a){
String   s= "class   S{public   static   void   main(String[]a){
String   s=;char   c= ' '
;System.out.println(s.substring(0,52)+c+s+c+s.substring(52,61)+c+s.substring(61));}} ";
char   c= ' " ';
System.out.println(s.substring(0,52)+c+s+c+s.substring(52,61)+c+s.substring(61));}}