- package com.itheima.practice;
 
 -    
 
 -  import java.io.BufferedReader;  
 
 -  import java.io.Console;  
 
 -  import java.io.IOException;  
 
 -  import java.io.InputStreamReader;  
 
 -  import java.util.Scanner;  
 
 -  import javax.swing.JOptionPane;  
 
 -  import junit.framework.TestCase;  
 
 -    
 
 -  public class InputTest extends TestCase {  
 
 -        
 
 -      /** 
 
 -       * 利用字节输入流 
 
 -       */  
 
 -      public void testByteReader(){  
 
 -          String s = "";  
 
 -          System.out.println("ByteReader方式输入");  
 
 -          byte[] readIn = new byte[50];  
 
 -            
 
 -          int count = 0;  
 
 -          try{  
 
 -        
 
 -          System.out.println("you input:");  
 
 -          count = System.in.read(readIn);  
 
 -        
 
 -          }catch(Exception e){  
 
 -          e.printStackTrace();  
 
 -          }  
 
 -            
 
 -          System.out.println(new String(readIn, 0, count));   
 
 -      }  
 
 -        
 
 -      /** 
 
 -       * 利用字符输入流 
 
 -       */  
 
 -      public void testBufferedReader(){  
 
 -          String s = "";  
 
 -          BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  
 
 -          System.out.println("BufferReader方式输入");  
 
 -          try {  
 
 -              s = br.readLine();  
 
 -          } catch (IOException e) {  
 
 -              e.printStackTrace();  
 
 -          }  
 
 -    
 
 -          System.out.println(s);  
 
 -            
 
 -            
 
 -      }  
 
 -   /** 
 
 -    * 扫描器类(Scanner)从控制台中读取字符串 
 
 -    */  
 
 -      public void testScanner(){  
 
 -          String s = "";  
 
 -          Scanner sc = new Scanner(System.in);  
 
 -    
 
 -          System.out.println("Scanner方式输入");  
 
 -          s = sc.next();  
 
 -          System.out.println(s);  
 
 -    
 
 -          }  
 
 -      /** 
 
 -       * 对话框输入数据 
 
 -       */  
 
 -      public void testJOptionPane(){  
 
 -        
 
 -          System.out.println("JOptionPane方式输入");  
 
 -          String name = JOptionPane.showInputDialog("what is your name?");  
 
 -            
 
 -          System.out.println(name);  
 
 -    
 
 -          }  
 
 -      public void testConsole(){  
 
 -          /** jdk1.6新功能 
 
 -           * Java要与Console进行交互,不总是能得到可用的Java 
 
 -           * Console类的。一个JVM是否有可用的Console,依赖于底层平台和JVM如何被调用。
 
 -           * 如果JVM是在交互式命令行(比如Windows的cmd)
 
 -           * 中启动的,并且输入输出没有重定向到另外的地方,那么就我们可以得到一个可用的Console实例。 
 
 -           * 当你有Eclipse或NetBean中运行以上代码时Console中将会有以下文字输出: Console is unavailable. 
 
 -           *表示Java程序无法获得Console实例,是因为JVM不是在命令行中被调用的,或者输入输出被重定向了。 
 
 -           */  
 
 -          Console console=System.console();  
 
 -            if (console == null) {     
 
 -                  throw new IllegalStateException("不能使用控制台");     
 
 -              }     
 
 -          String s= console.readLine("输入你的参数");  
 
 -          System.out.println(s);  
 
 -    
 
 -          }  
 
 -        
 
 -      public static void main(String[]  args){  
 
 -          /** 
 
 -           * 主函数参数输入 
 
 -           */  
 
 -          System.out.println(args[0]);  
 
 -      }  
 
 -        
 
 -  }
 
  复制代码 |