Untitled

 avatar
unknown
java
3 years ago
3.4 kB
4
Indexable
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Test {
	public static void main(String[] args) {
		 String[] cmdAttribs = null;
		 
		 cmdAttribs = new String[] { "cmd.exe", "/C", "fsutil" };
		 try {
		     performCommand(cmdAttribs, Integer.MAX_VALUE);
		 } catch (IOException e) {
		     System.out.println(e.getMessage());
		 }
	}
	
	/**
     * Performs the os command.
     *
     * @see org.apache.commons.io.FileSystemUtils#performCommand
     *
     * @param cmdAttribs
     *        the command line parameters
     * @param max
     *        The maximum limit for the lines returned
     * @return the parsed data
     * @throws IOException
     *         if an error occurs
     */
    private static List<String> performCommand(String[] cmdAttribs, int max)
            throws IOException {

        List<String> lines = new ArrayList<String>(20);
        Process proc = null;
        InputStream in = null;
        OutputStream out = null;
        InputStream err = null;
        BufferedReader inr = null;
        try {
            proc = openProcess(cmdAttribs);
            in = proc.getInputStream();
            out = proc.getOutputStream();
            err = proc.getErrorStream();
            // inr = new BufferedReader(new InputStreamReader(in,
            // "Windows-31J"));
            inr = new BufferedReader(new InputStreamReader(in));
            String line = inr.readLine();
            while (line != null && lines.size() < max) {
                line = line.toLowerCase().trim();
                lines.add(line);
                line = inr.readLine();
            }

            proc.waitFor();
            if (proc.exitValue() != 0) {
                // os command problem, throw exception
                throw new IOException("Command line returned OS error code '"
                        + proc.exitValue() + "' for command "
                        + Arrays.asList(cmdAttribs));
            }
            if (lines.size() == 0) {
                // unknown problem, throw exception
                throw new IOException("Command line did not return any info "
                        + "for command " + Arrays.asList(cmdAttribs));
            }
            return lines;

        } catch (InterruptedException ex) {
            throw new IOException(
                    "Command line threw an InterruptedException '"
                            + ex.getMessage() + "' for command "
                            + Arrays.asList(cmdAttribs));
        } finally {
            in.close();
            out.close();
            err.close();
            inr.close();
            if (proc != null) {
                proc.destroy();
            }
        }
    }
    
    /**
     * Opens the process to the operating system.
     *
     * @see org.apache.commons.io.FileSystemUtils#openProcess
     *
     * @param cmdAttribs
     *        the command line parameters
     * @return the process
     * @throws IOException
     *         if an error occurs
     */
    private static Process openProcess(String[] cmdAttribs) throws IOException {
        return Runtime.getRuntime().exec(cmdAttribs);
    }
}
Editor is loading...