Java将“[”和“]”写入文本文件而不是“[”和“]”

时间:2018-05-12 05:31:20

标签: java java-io

我将输入字符串作为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();

看起来有点复杂,但一般来说,我将这三段代码添加到源代码输入的主要方法中。

//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&#91;&#93; 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”变量/数组的所有内容。

注意:

  1. CQ.sendPrivateMsg(msgReceiver,message)用于将消息发送给接收者,这是正常的。

  2. “msg”变量从聊天软件传递,我只能使用其API发送/接收消息,而我没有源代码......

  3. 此程序是聊天软件插件的一部分。

    ...
    ...
    ...
    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
    &#91
    
    //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循环来打印它时它很好。

    更新

    我解决了它,通过[或]来替换[和其他奇怪的代码。

    评论中给出的建议非常有帮助。欣赏!

1 个答案:

答案 0 :(得分:0)

似乎味精是作为HTML编码的字符串接收的。

您可以使用Apache Commons https://aws.amazon.com/对该字符串进行解码。