BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / java / #57783同步于 2017/10/23
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Java机器人发帖

一道巧妙的题

BILL
2017/10/23镜像同步12 回复
要求: 编写一段java代码,其功能是将代码本身输出到一个out.txt文件里。代码里必须包括中文,而且必须能在out.txt中正常显示,如果能显示行号可以加分。 这道题感觉非常巧妙,第一次见到要求输出代码本身到一个文件。网上有类似的代码,直接输出代码本身,并不是到文件,但是不知道如何输出到文件。 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]); } } 求大神们指导![ema1]
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
wht机器人#1 · 2017/10/23
看一下java io的内容,用printwriter就行,注意输出编码用utf-8
BILL机器人#2 · 2017/10/23
求详细怎么改? 【 在 wht 的大作中提到: 】 : 看一下java io的内容,用printwriter就行,注意输出编码用utf-8 : -- : :
nanguohao机器人#3 · 2017/10/23
直接读程序文件不能输出?
BILL机器人#4 · 2017/10/23
【 在 nanguohao 的大作中提到: 】 : 直接读程序文件不能输出? 是要求输出到一个文件里
nemo94机器人#5 · 2017/10/23
【 在 BILL 的大作中提到: 】 : 要求: : 编写一段java代码,其功能是将代码本身输出到一个out.txt文件里。代码里必须包括中文,而且必须能在out.txt中正常显示,如果能显示行号可以加分。 : [upload=1][/upload] : ...................
nanguohao机器人#6 · 2017/10/23
public class 输出程序到文件 { public static void main(String[] args) throws IOException{ File file=new File("src/Other/out.txt"); file.createNewFile(); InputStreamReader r=new InputStreamReader(new FileInputStream("src/Other/输出程序到文件.java"), Charset.forName("GBK")); OutputStreamWriter w=new OutputStreamWriter(new FileOutputStream(file), Charset.forName("GBK")); int c; while ((c=r.read())!=-1) { w.write(c); w.flush(); } w.close(); r.close(); } }
GOON机器人#7 · 2017/10/23
不会写java,但是会一点拍黄片。尝试了下好像还行,感觉属于文件操作吧。加行号的话,需要一个按行读取的函数 ``` <?php /** * Created by PhpStorm. * User: Mr_GooN * Date: 2017/6/15 * Time: 21:52 */ namespace script; $test = new TestScript(); $test->outputFileContent(); class TestScript { public function outputFileContent() { //尝试 $content = file_get_contents(__FILE__); $lines = explode("\r\n", $content); $content_with_number = ''; foreach ($lines as $line => $code) { $content_with_number .= $line+1 . " $code\r\n"; } file_put_contents('d:/a.txt', $content_with_number); } } ``` 效果图: 【 在 BILL 的大作中提到: 】 : 要求: : 编写一段java代码,其功能是将代码本身输出到一个out.txt文件里。代码里必须包括中文,而且必须能在out.txt中正常显示,如果能显示行号可以加分。 : [upload=1][/upload] : ...................
ml3615556机器人#8 · 2017/10/23
不是应该先反编译class文件然后输出?
lance6716机器人#9 · 2017/10/23
哈哈哈哈哈哈 【 在 nanguohao 的大作中提到: 】 : public class 输出程序到文件 { : public static void main(String[] args) throws IOException{ : File file=new File("src/Other/out.txt"); : ...................