0

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

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

1 回答 1

0

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

于 2013-07-25T19:56:22.627 回答