使用Power Shell命令将空字符替换为Java中的空格

问题描述

我试图通过运行Java程序中的powershell命令,用空格替换文本文件中的空字符。下面是我的代码段。

Runtime.getRuntime()。exec(windowsCommand)方法在内部使用StringTokenizer,它将字符串作为标记分隔符。由于这个原因,{$ _。Replace(“ 0”,“”)}被视为两个令牌。

任何解决此问题的指针或任何其他方法,我们都可以从1 GB的文件中替换空字符而无需读取该文件。在阅读之前,我必须替换空字符。

String windowsCommand = String.format(
                "powershell Get-Content -Path %s -TotalCount 20 | Foreach-Object {$_.Replace(\"`0\",\" \")} | Set-Content -Path %s",fitFilePath,fitFilePathUpdated);
        
        String shellCommand = String.format("sh -c tr '\\000' ' ' < \"%s\" > \"%s\"",fitFilePathUpdated);
        
    Process process = null;
    if (isWindows) {
        process = Runtime.getRuntime().exec(windowsCommand);
    } else {
        process = Runtime.getRuntime().exec(shellCommand);
    }

谢谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)