Java从控制台读入数据的几种方法


这里记录Java中从控制台读入信息的几种方式,已备后查!

(1)JDK 1.4(JDK 1.5和JDK 1.6也都兼容这种方法)

 
  1. public class TestConsole1 {  
  2.     public static void main(String[] args) {  
  3.         String str = readDataFromConsole("Please input string:);  
  4.         System.out.println("The information from console: + str);  
  5.     }  
  6.   
  7.     /** 
  8.      * Use InputStreamReader and System.in to read data from console 
  9.      *  
  10.      * @param prompt 
  11.      *             
  12.      * @return input string 
  13.      */  
  14.     private static String readDataFromConsole(String prompt) {  
  15.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  
  16.         String str = null;  
  17.         try {  
  18.             System.out.print(prompt);  
  19.             str = br.readLine();  
  20.   
  21.         } catch (IOException e) {  
  22.             e.printStackTrace();  
  23.         }  
  24.         return str;  
  25.     }  
  26. }  


 

(2)JDK 1.5(利用Scanner进行读取)

 
  1. public class TestConsole2 {  
  2.     public static void main(String[] args) {  
  3.         String str = readDataFromConsole("Please input string:");  
  4.         System.out.println("The information from console:" + str);  
  5.     }  
  6.   
  7.     /** 
  8.      * Use  java.util.Scanner to read data from console 
  9.      *  
  10.      * @param prompt 
  11.      *  
  12.      * @return input string 
  13.      */  
  14.     private static String readDataFromConsole(String prompt) {  
  15.         Scanner scanner = new Scanner(System.in);  
  16.         System.out.print(prompt);  
  17.         return scanner.nextLine();  
  18.     }  
  19. }  


 
Scanner还可以很方便的扫描文件,读取里面的信息并转换成你要的类型,比如对“2 2.2 3.3 3.33 4.5 done”这样的数据求和,见如下代码:
 
  
  1. public class TestConsole4 {  
  2.   
  3.     public static void main(String[] args) throws IOException {  
  4.         FileWriter fw = new FileWriter("num.txt");  
  5.         fw.write("2 2.2 3.3 3.33 4.5 done");  
  6.         fw.close();  
  7.   
  8.         System.out.println("Sum is "+scanFileForSum("num.txt"));  
  9.     }  
  10.   
  11.     public static double scanFileForSum(String fileName) throws IOException {  
  12.         double sum = 0.0;  
  13.         FileReader fr = null;  
  14.         try {  
  15.             fr = new FileReader(fileName);  
  16.             Scanner scanner = new Scanner(fr);  
  17.               
  18.             while (scanner.hasNext()) {  
  19.                 if (scanner.hasNextDouble()) {  
  20.                     sum = sum + scanner.nextDouble();  
  21.   
  22.                 } else {  
  23.                     String str = scanner.next();  
  24.   
  25.                     if (str.equals("done")) {  
  26.                         break;  
  27.                     } else {  
  28.                         throw new RuntimeException("File Format is wrong!");  
  29.                     }  
  30.   
  31.                 }  
  32.             }  
  33.   
  34.         } catch (FileNotFoundException e) {  
  35.             throw new RuntimeException("File " + fileName + " not found!");  
  36.         } finally {  
  37.             if (fr != null)  
  38.                 fr.close();  
  39.         }  
  40.         return sum;  
  41.     }  
  42. }  
  • 1
  • 2
  • 下一页

相关内容