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

时间:2013-07-18 16:31:57

标签: java stream console

我正在尝试捕获控制台程序的输出并将输出的覆盖行写入另一个程序将读取的文件,逐行写入此文件(该文件一次只能包含一行)但是当我制作这段代码并尝试运行它时,它无效。这个过程很完美,但文件没有被创建,写入,而且我没有得到任何System.out.println的“Streaming:blah blah blah blah”

您可以阅读以下代码或使用此代码: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();
    }
}
    }

1 个答案:

答案 0 :(得分:0)

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