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

标签 java netbeans wrapper executable jsmooth

我这里遇到了一个奇怪的问题。我有一个 Java 小程序,可以过滤 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++;
    }

最佳答案

我过去在使用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("§", "");

或者专门使用该字符的实际 unicode 字符串U+00A7,如下所示

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

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

关于java - 使用 JSmooth 包装 .jar 时字符串搜索中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47269685/

相关文章:

java - 如何在 Eclipse 中自动生成 *-javadoc.jar?

java - 如何在java中创建实时跟踪搜索字段

Java NetBeans GUI 按键事件发送

c - Photoshop CS5对Wintab驱动的使用

java.lang.NoSuchMethodError 错误

java - 类加载样式的困惑

java - TestNG Dataprovider - 过滤测试数据

macos - 在 Mac 上的 Netbeans 中的选项卡之间切换?

c# - 包装 INLINE 函数

c# - 用 C++ CLI 编写的 DOTNET 包装器 native 传递结构的最佳方式?