Java Text File Read Line readLinesFromCommand(String command[], List buffer)

Here you can find the source of readLinesFromCommand(String command[], List buffer)

Description

read Lines From Command

License

Open Source License

Declaration

public static int readLinesFromCommand(String command[],
            List<String> buffer) 

Method Source Code

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

import java.util.Arrays;
import java.util.List;

import java.io.Closeable;

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

public class Main {
    public static int readLinesFromCommand(String command[],
            List<String> buffer) {
        if (command == null || buffer == null) {
            return -2;
        }// w  w  w .j a  v  a2  s .  c o m
        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. readLines(String text)
  2. readLines(String text)
  3. readLines2Lowcase(String inputFile, ArrayList lines)
  4. readLinesAsList(String filename)
  5. readLinesFileSimple(final File inFile, final int prefixMode, final String prefix)
  6. readLinesNoComments(String fileName)
  7. readLinesRaw(final File inFile)
  8. readLinesTrimmedNoComment(final String fileName, final String commentString)
  9. readLinesWithPattern(File file, Pattern pattern)