Java:无法捕获控制台程序的控制台输出(Blockland.exe)



我试图捕获控制台程序的输出,并将输出的覆盖行写入另一个程序将读取的文件,逐行写入该文件(该文件一次只能包含一行),但当我编写并尝试运行此代码时,它没有起作用。这个过程开始得很好,但文件没有被创建、写入,我也没有得到任何"流媒体:等等"的System.out.println

您可以阅读下面的代码或使用此粘贴框:http://pastebin.com/raw.php?i=Yahsqxma

    import java.io.*;
    import java.util.Scanner;

    public class OpenRC {
static BufferedReader consoleInput = null;
static String os = System.getProperty("os.name").toLowerCase();
static Process server;
public static void main(String[] args) throws IOException {
    // OpenRC by Pacnet2013
    System.out.println(System.getProperty("user.dir"));
    if(os.indexOf("win") >= 0) {
        os = "Windows";
    }
    else if(os.indexOf("mac") >= 0) {
        os = "Mac";
    }
    else if(os.indexOf("nux") >= 0) {
        os = "Linux";
    }
    switch(os){
        case "Linux" : //cause I need WINE      
            File file = new File(System.getProperty("user.dir") + "/OpenRC.txt");
            try {
                Scanner scanner = new Scanner(file);
                    String path = scanner.nextLine();
                    System.out.println("Got BlocklandEXE - " + path);
                    String port = scanner.nextLine();
                    System.out.println("Got port - " + port);
                scanner.close();
                server = new ProcessBuilder("wine", path + "Blockland.exe", "ptlaaxobimwroe", "-dedicated", "-port" + port).start();        
                if(consoleInput != null)
                    consoleInput.close();
                consoleInput = new BufferedReader(new InputStreamReader(server.getInputStream()));
                streamLoop();
            } catch (FileNotFoundException e) {
                System.out.println("You don't have an OpenRC Config file OpenRC.txt in the directory of this program");
            }
    }
}
public static void streamConsole() 
{
    String line = "";
    int numLines = 0;
    try
    {
        if (consoleInput != null)
        {
            while((line = consoleInput.readLine()) != null && consoleInput.ready())
            {
                numLines++;
            }
        }
    }
    catch (IOException e)
    {
        System.out.println("There may be a problem - An IOException (java.io.IOException) was caught so some lines may not display / display correctly");
    }
    if(!line.equals("") && !(line == null))
    {
        System.out.println("Streaming" + numLines + line);
        writeToFile(System.getProperty("user.dir"), line);
    }
}
public static void streamLoop()
{
    try
    {
        Thread.sleep(5000);
    }
    catch (InterruptedException e)
    {
        System.out.println("A slight problem may have happened while trying to read a command");
    }
    streamConsole();
    streamLoop(); //it'll go on until you close this program
}
public static void writeToFile(String filePath, String content)
{
    try {
        File file = new File(filePath);
        if (!file.exists()) {
            file.createNewFile();
            System.out.println("Creating new stream text file");
        }
        FileWriter writer = new FileWriter(file.getAbsoluteFile());
        BufferedWriter bw = new BufferedWriter(writer);
        bw.write(content);
        bw.close();
        System.out.println("Wrote stream text file");
    } catch (IOException e) {
        e.printStackTrace();
    }
}
    }

您正在运行一个DOS控制台应用程序,它不一定会写入stdout或stderr,但它会写入"控制台"。几乎不可能可靠地捕获"控制台"输出。我见过的唯一一个能够捕获控制台输出的工具是Don Libes的expect,它可以进行各种破解。

相关内容

  • 没有找到相关文章

最新更新