Your location:Tech News>News>Script>Several ways to read data from a Java console summary

Latest News

Several ways to read data from a Java console summary

Author:fsadmin

Views:

  Here are several ways Java recording read information from the console, the investigation has been prepared!

  (1) JDK 1.4 (JDK 1.5 and JDK 1.6 are also compatible with this method)

  public class TestConsole1 {

  public static void main (String [] args) {

  String str = readDataFromConsole ( "Please input string :);

  System.out.println ( "The information from console: + str);

  }

  private static String readDataFromConsole (String prompt) {

  BufferedReader br = new BufferedReader (new InputStreamReader (System.in));

  String str = null;

  try {

  System.out.print (prompt);

  str = br.readLine ();

  } Catch (IOException e) {

  e.printStackTrace ();

  }

  return str;

  }

  }

  (2) JDK 1.5 (read using Scanner)

  public class TestConsole2 {

  public static void main (String [] args) {

  String str = readDataFromConsole ( "Please input string:");

  System.out.println ( "The information from console:" + str);

  }

  private static String readDataFromConsole (String prompt) {

  Scanner scanner = new Scanner (System.in);

  System.out.print (prompt);

  return scanner.nextLine ();

  }

  }

  Scanner also can easily scan documents, read the information inside and converted to the type you want, such as on "22.twenty three.33.334.5 done "Such data summation, see the following code:

  public class TestConsole4 {

  public static void main (String [] args) throws IOException {

  FileWriter fw = new FileWriter ( "num.TXT");

  fw.write ( "2 2.twenty three.33.334.5 done ");

  fw.close ();

  System.out.println ( "Sum is" + scanFileForSum ( "num.TXT"));

  }

  public static double scanFileForSum (String fileName) throws IOException {

  double sum = 0.0;

  FileReader fr = null;

  try {

  fr = new FileReader (fileName);

  Scanner scanner = new Scanner (fr);

  while (scanner.hasNext ()) {

  if (scanner.hasNextDouble ()) {

  sum = sum + scanner.nextDouble ();

  } Else {

  String str = scanner.next ();

  if (str.equals ( "done")) {

  break;

  } Else {

  throw new RuntimeException ( "File Format is wrong!");

  }

  }

  }

  } Catch (FileNotFoundException e) {

  throw new RuntimeException ( "File" + fileName + "not found!");

  } Finally {

  if (fr != Null)

  fr.close ();

  }

  return sum;

  }

  }

  (3) JDK 1.6 (use java.io.Console reading)

  JDK6 provides java.io.Console class special equipment to access the character-based console.

  If you want your program to interact at Terminal cmd under Windows or Linux and you can do it with the Console class.(Similar to the System.in and System.out)

  But we do not always get the available Console, a JVM for available Console depend on how the underlying platform and the JVM is called.

  If the JVM is the interactive command line (such as Windows, cmd) is started, and the input and output is not redirected to another place, you can get a usable instance Console.

  In the case of using the IDE, can not be acquired Console instance, because of the environment in the IDE, redirecting the standard input and output streams, is also the input and output on the system console is redirected to the IDE console

  public class TestConsole3 {

  public static void main (String [] args) {

  String str = readDataFromConsole ( "Please input string:");

  System.out.println ( "The information from console:" + str);

  }

  private static String readDataFromConsole (String prompt) {

  Console console = System.console ();

  if (console == null) {

  throw new IllegalStateException ( "Console is not available!");

  }

  return console.readLine (prompt);

  }

  }

  Console class there is a characteristic that is devoted to the password (enter no echo) and other safety characters have been processed.We specialize readpassword () method, the specific application, see the following code:

  public class TestConsole5 {

  public static void main (String [] args) {

  Console console = System.console ();

  if (console == null) {

  throw new IllegalStateException ( "Console is not available!");

  }

  while (true) {

  String username = console.readLine ( "Username:");

  char [] password = console.readPassword ( "Password:");

  if (username.equals ( "Chris") && String.valueOf (password).equals ( "GoHead")) {

  console.printf ( "Welcome to Java Application% 1 $ s.\ N ", username);

  // Use immediately after emptying the array to reduce its footprint in memory time, enhance security

  password = null;

  System.exit (-1);

  }

  else {

  console.printf ( "Invalid username or password.\ N ");

  }

  }

  }

  }

  That''s all in several ways small as we bring Java to read data from the console summary, and we want to help, a lot of support scripting House ~

  Methods Java methods to obtain the current time timestamp of the date of obtaining the morning of Java Java methods to obtain UTC time analysis method Detailed java get a summary of a variety of ways Date Time Java acquisition time of year, month, day: You may be interested in articles java get the current time and the day before the date (implementation code) java library management system java console output by row and read the file to the console method Java console input array and reverse output instance of a Java method to control print acquisition time examples Code table

Recommend article

Relate article