import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; public class DemoScanner { //打印 public static void printScanner() { Scanner s = new Scanner(System.in); System.out.println("请输入字符串:"); while (true) { String line = s.nextLine(); if (line.equals("exit")) break; System.out.println(">>>" + line); } } //自定义换行符 public static void lnScanner() { Scanner s = new Scanner("123 asdf sd 45 789 sdf asdfl,sdf.sdfl,asdf ......asdfkl las"); s.useDelimiter(" |,|\."); while (s.hasNext()) { System.out.println(s.next()); } } //InputStreamReader类比scanner效率高,其中的BufferedReader类中对象只生成一次,以后可以重复使用,这就是效率高的原因。 private static String readString(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; } public static void main(String[] args) { String str = readString("请输入字符串:"); System.out.println("readString6 方法的输入:" + str); } }