使用JSmooth包装.jar时字符串搜索中断

时间:2017-11-13 17:01:26

标签: java netbeans wrapper executable jsmooth

我在这里遇到了一个古怪的问题。我有一个小程序,可以过滤Minecraft日志文件,使它们更容易阅读。在这些日志的每一行中,通常有多个字符“§”的实例,它返回十六进制值FFFD。

我使用:

过滤掉这个角色(以及它后面的角色)
currentLine = currentLine.replaceAll("\uFFFD.", "");

现在,当我通过NetBeans运行程序时,它会起作用。我的线路输出看起来像这样:

CxndyAnnie: Mhm
CxndyAnnie: Sorry

但是当我构建.jar文件并使用JSmooth将其包装到.exe文件中时,当我运行.exe时,该字符不再被过滤掉,我的行看起来像这样:

§e§7[§f$65§7] §1§nCxndyAnnie§e: Mhm
§e§7[§f$65§7] §1§nCxndyAnnie§e: Sorry

(注意:额外的方括号和$ 65显示,因为它们的过滤取决于特殊字符,并且首先删除后面的字符)

任何想法为什么在通过JSmooth之后它将不再起作用?是否有不同的方法来进行文本替换以保留其功能?

顺便说一句,我还尝试使用

删除此字符
currentLine = currentLine.replaceAll("§.", "");

但这在Netbeans中不起作用,也不起作为.exe。

我将继续完成下面的完整方法:

 public static String[] filterLines(String[] allLines, String filterType, Boolean timeStamps) throws IOException {
    String currentLine = null;
    FileWriter saveFile = new FileWriter("readable.txt");
    String heading;
    String string1 = "[L]";
    String string2 = "[A]";
    String string3 = "[G]";
    if (filterType.equals(string1)) {
        heading = "LOCAL CHAT LOGS ONLY \r\n\r\n";
    }
    else if (filterType.equals(string2)) {
        heading = "ADVERTISING CHAT LOGS ONLY \r\n\r\n";
    }
    else if (filterType.equals(string3)) {
        heading = "GLOBAL CHAT LOGS ONLY \r\n\r\n";
    }
    else {
        heading = "CHAT LINES CONTAINING \"" + filterType + "\" \r\n\r\n";    
    }
    saveFile.write(heading);

    for (int i = 0; i < allLines.length; i++) {
        if ((allLines[i] != null ) && (allLines[i].contains(filterType))) {
            currentLine = allLines[i];
            if (!timeStamps) {
                currentLine = currentLine.replaceAll("\\[..:..:..\\].", "");
            }
            currentLine = currentLine.replaceAll("\\[Client thread/INFO\\]:.", "");
            currentLine = currentLine.replaceAll("\\[CHAT\\].", "");
            currentLine = currentLine.replaceAll("\uFFFD.", "");
            currentLine = currentLine.replaceAll("\\[A\\].", "");
            currentLine = currentLine.replaceAll("\\[L\\].", "");
            currentLine = currentLine.replaceAll("\\[G\\].", "");
            currentLine = currentLine.replaceAll("\\[\\$..\\].", "");
            currentLine = currentLine.replaceAll(".>", ":");
            currentLine = currentLine.replaceAll("\\[\\$100\\].", "");
            saveFile.write(currentLine + "\r\n");
            //System.out.println(currentLine);
        }
    }
    saveFile.close();
    ProcessBuilder openFile = new ProcessBuilder("Notepad.exe", "readable.txt");
    openFile.start();
    return allLines;
}

最终编辑

以防万一有人偶然发现并且需要知道最终的工作原理,这里是代码片段,我从文件中提取行并将其重新编码为工作:

    BufferedReader fileLines;
    fileLines = new BufferedReader(new FileReader(file));
    String[] allLines = new String[numLines];
    int i=0;
    while ((line = fileLines.readLine()) != null) {
        byte[] bLine = line.getBytes();
        String convLine = new String(bLine, Charset.forName("UTF-8"));
        allLines[i] = convLine;
        i++;
    }

1 个答案:

答案 0 :(得分:0)

我在过去使用minecroft日志也有这样的问题,我不记得确切的细节,但问题归结为文件格式问题,其中UTF8编码正常工作但其他一些文本编码包括系统默认无法正常工作。

首先:

确保在从文件中读取byteArray时指定UTF8编码,以便allLines包含正确的信息,如下所示:

Path fileLocation = Paths.get("C:/myFileLocation/logs.txt");
byte[] data = Files.readAllBytes(fileLocation);
String allLines = new String(data , Charset.forName("UTF-8"));

第二

使用\uFFFD不起作用,因为\uFFFD仅用于替换Unicode中未知或不可表示的值的传入字符。

但是如果您使用了正确的编码(在我的第一点中显示),则\uFFFD不是必需的,因为值§在unicode中已知,因此您只需使用

currentLine.replaceAll("§", "");

或者特别使用该字符U+00A7的实际unicode字符串,如此

currentLine.replaceAll("\u00A7", "");

或者只是在代码中使用这两行。