有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

unix空输出文件是在Java进程运行时生成的

我的某段代码有一些问题。应该发生的是,Java程序接受一些预先确定的变量,并使用UNIX的“sed”函数替换预先编写的shell脚本中的字符串“AAA”和“BBB”。我有三种方法可以做到这一点:一种是使用“sed”替换文件中的字符串,并将输出写入另一个文件;使用“rm”命令删除原始文件的程序;以及使用“mv”将输出文件重命名为原始文件的名称。在三个不同的目录中有三个shell脚本副本,每个副本都应该替换为它自己的特定变量

应该对所有三个shell脚本文件进行替换,但仅对两个shell脚本文件进行替换。在第三个shell脚本中,似乎进程没有完成,因为该文件的字节大小为0。未被替换的文件是完全随机的,因此在每次运行期间都不是同一个文件不工作

我不知道为什么会发生这个错误。有人有什么可能的解决办法吗?代码如下:

    public void modifyShellScript(String firstParam, String secondParam, int thirdParam, int fourthParam, String outfileDirectoryPath) throws IOException{
    String thirdDammifParamString = "";
    String fourthDammifParamString = "";
    thirdDammifParamString = Integer.toString(thirdDammifParam);
    fourthDammifParamString = Integer.toString(fourthDammifParam);
    String[] cmdArray3 = {"/bin/tcsh","-c", "sed -e 's/AAA/"+firstDammifParam+"/' -e 's/BBB/"+secondDammifParam+"/' -e 's/C/"+thirdDammifParamString+"/' -e 's/D/"+fourthDammifParam+"/' "+outfileDirectoryPath+"runDammifScript.sh > "+outfileDirectoryPath+"runDammifScript.sh2"};
    Process p;
    p = Runtime.getRuntime().exec(cmdArray3);
}

public void removeOriginalShellScript(String outfileDirectoryPath) throws IOException{
    String[] removeCmdArray = {"/bin/tcsh", "-c", "rm "+outfileDirectoryPath+"runDammifScript.sh"};
    Process p1;
    p1 = Runtime.getRuntime().exec(removeCmdArray);
}

public void reconvertOutputScript(String outfileDirectoryPath) throws IOException{
    String[] reconvertCmdArray = {"/bin/tcsh","-c","mv "+outfileDirectoryPath+"runDammifScript.sh2 "+outfileDirectoryPath+"runDammifScript.sh"};
    Process reconvert; 
    reconvert = Runtime.getRuntime().exec(reconvertCmdArray);
}

共 (1) 个答案

  1. # 1 楼答案

    如果你还没有,看看When Runtime.exec() won't。一个或多个Process可能挂起,因为您没有使用输出和错误流。特别是,看看那篇文章例子中的StreamGobbler

    也可能是您忘记在outfileDirectoryPath中包含尾部斜杠。阅读流程的错误流,查看出了什么问题:

    InputStream err = p.getErrorStream();
    // read the stream and print its contents to the console, or whatever
    

    请记住,您需要在单独的线程中读取流

    也就是说,我个人只会用Java本机完成所有这些,而不是依赖外部的、特定于平台的依赖关系

    对于子串替换,read the file to a String,然后使用String.replace和/或String.replaceAll

    您可以用对File.delete的调用替换removeOriginalShellScript的正文:

    public void removeOriginalShellScript(String outfileDirectoryPath) throws IOException{
        File f = new File(outfileDirectoryPath, "runDammifScript.sh");
        f.delete();
    }
    

    您可以用对Files.move的调用替换reconvertOutputScript的正文:

    public void reconvertOutputScript(String outfileDirectoryPath) throws IOException{
        File src = new File(outfileDirectoryPath, "runDammifScript.sh2");
        File dst = new File(outfileDirectoryPath, "runDammifScript.sh");
        Files.move(src, dst);
    }
    

    或者只需将removeOriginalShellScript和reconvertOoutputScript替换为对Files.move的调用,并指定REPLACE_EXISTING选项:

    File src = new File(outfileDirectoryPath, "runDammifScript.sh2");
    File dst = new File(outfileDirectoryPath, "runDammifScript.sh");
    Files.move(src, dst, REPLACE_EXISTING);