我将输入字符串作为Java源代码,对其进行一些编辑,并在Java中生成一个.java文件。
这是我的代码。
BufferedWriter writer = new BufferedWriter(new FileWriter("javacode.java"));
//msg = msg.substring(4); //ignore this
String newcontent = "import java.io.BufferedWriter;import java.io.ByteArrayOutputStream;import java.io.FileWriter;import java.io.PrintStream;";
char[] content = msg.toCharArray();
int j = msg.indexOf("String[] args") + 14;
boolean inMain = false;
for (int x=0;x<content.length;x++) {
if (x == j) {
inMain = true;
if (content[j] != '{') {
j += 1;
newcontent += String.valueOf(content[x]);
continue;
}
newcontent += String.valueOf(content[x]);
String prefix = "ByteArrayOutputStream consoleStorage = new ByteArrayOutputStream();PrintStream newConsole = System.out;System.setOut(new PrintStream(consoleStorage));";
newcontent += prefix;
}
else if (content[x] == '}' && inMain) {
String post = "String str = consoleStorage.toString();try {BufferedWriter writer = new BufferedWriter(new FileWriter(\"javaoutput.txt\"));writer.write(str);writer.close();} catch (Exception e) {}";
newcontent += post;
newcontent += String.valueOf(content[x]);
inMain = false;
}
else {
newcontent += String.valueOf(content[x]);
}
}
writer.write(newcontent);
writer.close();
看起来可能有点复杂,但是一般来说,我将这三段代码添加到源代码输入的main方法中。
//At the beginning of the program, insert the following code
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.FileWriter;
import java.io.PrintStream;
...
...
...
//At the beginning of the main method, insert the following code
ByteArrayOutputStream consoleStorage = new ByteArrayOutputStream();
PrintStream newConsole = System.out;
System.setOut(new PrintStream(consoleStorage));
...
...
...
//At the end of the main method, insert the following code
String str = consoleStorage.toString();
try {
BufferedWriter writer = new BufferedWriter(new FileWriter("javaoutput.txt"));
writer.write(str);
writer.close();
} catch (Exception e) {}
但是,当我通过一个简单的Hello World示例进行测试时,得到了这个.java文件。
这是我的源代码输入(“ msg”变量,它是一个简单的字符串)
public class myClass {
public static void main(String[] args) {
System.out.println("Hello");
}
}
这是我得到的。(我对该文件进行了重新格式化以使其外观更好)
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.FileWriter;
import java.io.PrintStream;
public class myClass {
ByteArrayOutputStream consoleStorage = new ByteArrayOutputStream();
PrintStream newConsole = System.out;
System.setOut(new PrintStream(consoleStorage));
public static void main(String[] args) {
System.out.println("Hello");
String str = consoleStorage.toString();
try {
BufferedWriter writer = new BufferedWriter(new FileWriter("javaoutput.txt"));
writer.write(str);
writer.close();
} catch (Exception e) {}
}
}
如您所见,Java无法将“ [”和“]”放入文件中,而是写入“ []”,因此(可能)是“ int j = msg.indexOf(“ String [] args”)+ 14;“找不到主要方法。
我尝试了许多方法来解决此问题,包括用“ \\ [”等替换“ [”,但都没有用。老实说,我什至不确定“ []”是否会引起问题。
更新:
通过在我的程序中插入以下测试打印,我在不同阶段测试了“ msg”和“ content”变量/数组的所有内容。
注意:
CQ.sendPrivateMsg(msgReceiver,message)用于将消息发送到接收者,它工作正常。
“” msg“变量是从聊天软件传递来的,我只有它的API才能发送/接收消息,而我没有它的源代码...
该程序是聊天软件插件的一部分。
...
...
...
BufferedWriter writer = new BufferedWriter(new FileWriter("javacode.java"));
msg = msg.substring(4);
//test print 1
CQ.sendPrivateMsg(fromQQ, CC.at(fromQQ) + "\n" + msg);
String newcontent = "import java.io.BufferedWriter;import java.io.ByteArrayOutputStream;import java.io.FileWriter;import java.io.PrintStream;";
char[] content = msg.toCharArray();
//test print 2
CQ.sendPrivateMsg(fromQQ, CC.at(fromQQ) + "\n" + content[55] + content[56] + content[57] + content[58]);
//test print 3
String tempstr = new String();
for (int i=0;i<content.length;i++) {
tempstr += String.valueOf(content[i]);
}
CQ.sendPrivateMsg(fromQQ, CC.at(fromQQ) + "\n" + tempstr);
int j = msg.indexOf("String[] args") + 14;
...
...
...
实际结果如下所示
//test print 1: msg
public class myClass {
public static void main(String[] args) {
System.out.println("Hello");
}
}
//test print 2: char array, accessed each digit one by one
[
//test print 3: char array, concatenate in a loop and print out as a whole
public class myClass {
public static void main(String[] args) {
System.out.println("Hello");
}
}
[当我尝试访问char数组中的单个值时,似乎触发了该问题,但是当我使用for循环将其全部打印出来时,这很好。
更新:
我通过用[或]替换[和char数组中的其他奇怪代码,解决了它。
评论中的建议非常有帮助。赞赏!
似乎msg是作为HTML编码的字符串接收的。
您可以使用Apache Commons StringEscapeUtils.unescapeHtml4()对该字符串进行解码。