Console read and write Utils : Console « Development Class « Java






Console read and write Utils

        
//package org.loon.framework.game.simple.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * Copyright 2008 - 2009
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 * 
 * @project loonframework
 * @author chenpeng
 * @email:ceponline@yahoo.com.cn
 * @version 0.1
 */
final public class ConsoleUtils {

  private static BufferedReader reader;

  static {
    reader = new BufferedReader(new InputStreamReader(System.in));
  }

  private static String readData() {
    try {
      return reader.readLine();
    } catch (IOException ex) {
      return null;
    }
  }

  public static String readString() {
    return readData();
  }

  public static int readInteger() {
    return Integer.parseInt(readData());
  }

  public static float readFloat() {
    return Float.parseFloat(readData());
  }

  public static double readDouble() {
    return Double.parseDouble(readData());
  }

  public static long readLong() {
    return Long.parseLong(readData());
  }

  public static char[] readChars() {
    return readData().toCharArray();
  }

  public static String readSubstring(int start, int end) {
    return readData().substring(start, end);
  }

  public static int execute(String command) throws InterruptedException,
      IOException {
    Process process = Runtime.getRuntime().exec(command);
    process.waitFor();
    return process.exitValue();
  }

  public static Process exec(String command[], boolean stop)
      throws InterruptedException, IOException {
    Process p = Runtime.getRuntime().exec(command);
    if (stop)
      p.waitFor();
    return p;
  }

  public static Process exec(String exePath, List files, boolean stop)
      throws InterruptedException, IOException {
    List filePaths = new ArrayList();
    filePaths.add(exePath);
    for (int i = 0; i < files.size(); i++) {
      File file = (File) files.get(i);
      filePaths.add(file.getPath());
    }
    String command[] = (String[]) filePaths.toArray(new String[filePaths
        .size()]);
    return exec(command, stop);
  }

  public static Process exec(String command, boolean stop)
      throws InterruptedException, IOException {
    return exec(new String[] { command }, stop);
  }
}

   
    
    
    
    
    
    
    
  








Related examples in the same category

1.Turn System.out into a PrintWriterTurn System.out into a PrintWriter
2.Output to standard output and standard error; and input from standard input.Output to standard output and standard error; and input from standard input.
3.VarArgsDemo - show 1.5 variable argumentsVarArgsDemo - show 1.5 variable arguments
4.How to read from standard inputHow to read from standard input
5.How to deal with the console parametersHow to deal with the console parameters
6.Read input from consoleRead input from console
7.Command line input
8.Read an int from Standard Input
9.Read an int from Standard Input, using 1.5
10.SimpleCalc -- simple calculator using 1.5 java.util.Scanner
11.Show use of Argv to get an integer value from command lineShow use of Argv to get an integer value from command line
12.Java program to calculate the area of a circleJava program to calculate the area of a circle
13.Java program to demonstrate menu selectionJava program to demonstrate menu selection
14.Utility class for printing aligned columns of text
15.Processing the command line
16.A representation of the command line arguments passed to a Java class' main(String[]) method
17.Helper class for storing long command lines inside temporary file.
18.CommandLine Parser
19.This parses command line arguments optimized for ease of programmer use.