Java BufferedReader Read Line readLineFromCommand(String command[])

Here you can find the source of readLineFromCommand(String command[])

Description

read Line From Command

License

Open Source License

Declaration

public static String readLineFromCommand(String command[]) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Arrays;
import java.util.List;
import java.util.LinkedList;
import java.io.Closeable;

import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
    public static String readLineFromCommand(String command[]) {
        List<String> lines = new LinkedList<String>();
        int exitCode = readLinesFromCommand(command, lines);
        if (lines.isEmpty()) {
            return null;
        }// ww w . j a  v  a2  s.  c  o  m
        return lines.get(0);
    }

    public static int readLinesFromCommand(String command[],
            List<String> buffer) {
        if (command == null || buffer == null) {
            return -2;
        }
        Process cmdProc = null;
        try {
            cmdProc = Runtime.getRuntime().exec(command);
        } catch (IOException e) {
            System.out.printf("`%s` failed\n", Arrays.toString(command));
            e.printStackTrace();
            return -1;
        }
        BufferedReader cmdOutput = new BufferedReader(
                new InputStreamReader(cmdProc.getInputStream()));
        int returnCode = -1;
        try {
            returnCode = cmdProc.waitFor();
        } catch (InterruptedException e) {
            System.out.printf("Interrupted while waiting for `%s`\n",
                    Arrays.toString(command));
            e.printStackTrace();
            return -1;
        }
        String line = null;
        try {
            line = cmdOutput.readLine();
        } catch (IOException e) {
            System.out.printf("reading `%s` output failed\n",
                    Arrays.toString(command));
            return -1;
        }
        while (line != null) {
            buffer.add(line);
            try {
                line = cmdOutput.readLine();
            } catch (IOException e) {
                System.out.printf("reading `%s` output failed\n",
                        Arrays.toString(command));
                return -1;
            }
        }
        if (cmdProc != null) {
            boolean waited = false;
            while (!waited) {
                try {
                    cmdProc.waitFor();
                    waited = true;
                } catch (InterruptedException e) {
                }
            }
            if (cmdProc != null) {
                close(cmdProc.getOutputStream());
                close(cmdProc.getInputStream());
                close(cmdProc.getErrorStream());
                cmdProc.destroy();
            }
        }
        return returnCode;
    }

    private static void close(Closeable c) {
        if (c != null) {
            try {
                c.close();
            } catch (IOException e) {
                // ignored
            }
        }
    }
}

Related

  1. readLine(String s)
  2. readLine(String value, BufferedReader procout)
  3. readLineByLine(File file, char[][] terminators, boolean keepTerminators, Consumer readLineListener)
  4. readLineFile(File file)
  5. readLineFile(String filePath)
  6. readLineFromConsole()
  7. readLineHard(BufferedReader in)
  8. readLineList(String fileName)
  9. readLineToList(String pathName, String enCoding)